Course Spring annotation driven learning note AOP

AOP It refers to the programming method that dynamically cuts a piece of code into the specified location during the pro...
AOP

It refers to the programming method that dynamically cuts a piece of code into the specified location during the program running;
1. Import aop module; Spring AOP: (spring aspects)
2. Define a business logic class (MathCalculator); print the log when the business logic is running (before the method, after the method is running, exception occurs to the method, xxx)
3. Define a log aspects class: the methods in the aspects class need to dynamically sense where MathCalculator.div runs and then execute;

Notification method: Pre notification (@ Before): logStart: run before the target method (div) runs Post notification (@ After): logEnd: runs after the target method (div) runs (whether the method ends normally or abnormally) Return notification (@ AfterReturning): logReturn: run after the target method (div) returns normally Exception notification (@ AfterThrowing): logException: run after an exception occurs in the target method (div) Around notification (@ Around): dynamic agent, manually pushing the target method to run (joinpoint. Succeeded())


4. Mark when and where the target method of the tangent class runs (notification annotation);
5. Add the aspect class and business logic class (the class of the target method) to the container;
6. You must tell Spring which class is a tangent class (add an annotation to the tangent class: @ Aspect)
[7] Add @ EnableAspectJAutoProxy [enable annotation based aop mode] to the configuration class
Many @ EnableXXX in Spring

AOP development

Add business logic class

public class MathCalculator { public int div(int i,int j){ System.out.println("MathCalculator...div..."); return i/j; } }

Add tangent class

@Aspect public class LogAspects { //Extract common pointcut expressions //1. This kind of reference //2. Other tangent references @Pointcut("execution(public int com.atguigu.aop.MathCalculator.*(..))") public void pointCut(){}; //@Before before the target method; pointcut expression (specifies which method to cut in) @Before("pointCut()") public void logStart(JoinPoint joinPoint){ Object[] args = joinPoint.getArgs(); System.out.println(""+joinPoint.getSignature().getName()+"function...@Before:The parameter list is:{"+Arrays.asList(args)+"}"); } @After("com.atguigu.aop.LogAspects.pointCut()") public void logEnd(JoinPoint joinPoint){ System.out.println(""+joinPoint.getSignature().getName()+"end...@After"); } //JoinPoint must appear first in the parameter table @AfterReturning(value="pointCut()",returning="result") public void logReturn(JoinPoint joinPoint,Object result){ System.out.println(""+joinPoint.getSignature().getName()+"Normal return...@AfterReturning:Operation result:{"+result+"}"); } @AfterThrowing(value="pointCut()",throwing="exception") public void logException(JoinPoint joinPoint,Exception exception){ System.out.println(""+joinPoint.getSignature().getName()+"Abnormal... Exception information:{"+exception+"}"); } }

Add configuration class

@EnableAspectJAutoProxy @Configuration public class MainConfigOfAOP { //Add business logic class to container @Bean public MathCalculator calculator(){ return new MathCalculator(); } //Tangent class added to container @Bean public LogAspects logAspects(){ return new LogAspects(); } }

Test class

@Test public void test01() { AnnotationConfigApplicationContext applicationContext = new AnnotationConfigApplicationContext( MainConfigOfAOP.class); //1. Don't create objects yourself MathCalculator mathCalculator1 = new MathCalculator(); mathCalculator1.div(1, 1); System.out.println("----------------------------"); MathCalculator mathCalculator = applicationContext.getBean(MathCalculator.class); mathCalculator.div(1, 10); applicationContext.close(); }

Operation result

summary

Three steps to implement AOP
1) Add business logic components and Aspect classes to the container; tell Spring which is the Aspect class (@ Aspect)
2) Annotate each notification method on the tangent class to tell Spring when and where to run (pointcut expression)
3) Enable annotation based aop mode; @ EnableAspectJAutoProxy

Qin Da Ren 188 original articles published, 20 praised, 20000 visited+ Private letter follow

9 February 2020, 23:20 | Views: 4421

Add new comment

For adding a comment, please log in
or create account

0 comments