Spring annotation-driven development-AOP function test

Foreword

  Spring's AOP refers to the program is running dynamic piece of code will be cut into the specified position of the specified method for operating a dynamic programming approach [Agent].

AOP functional test

①Import AOP module

        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aspects</artifactId>
            <version>4.3.12.RELEASE</version>
        </dependency>

 

 

 ②Define logical components and aspect classes

Logical component

Print the log when the business logic is running (before the method, after the method runs, the method is abnormal, xxx)

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

}

Faceted

The methods in the aspect class need to dynamically sense where MathCalculator.div runs and execute;

/ ** 
 * Cut face class You 
must tell Spring which class is the cut face class (add a note to the cut face class: @Aspect)
* @Aspect: Tell Spring that the current class is a cut face class * * / @Aspect public class LogAspects { // extract Public entry point expression // 1, this class references pointCut () // 2, other aspect references com.atneusoft.springboot.aop.LogAspects.pointCut () @Pointcut ("execution (public int com.atneusoft.springboot .aop.MathCalculator. * (..)) " ) public void pointCut () {}; // @Before cut in before the target method; cut-in point expression (specify in which method to cut in)
    // Mark the target method of the aspect class when and where to run (notice annotation @Before \ @After \ @AfterReturning \ @AfterThrowing ) 
// Pre-notification (@Before): run
@Before before the target method (div) runs ("pointCut ()" ) public void logStart (JoinPoint joinPoint) { Object [] args = joinPoint.getArgs (); System.out.println ( "" + joinPoint.getSignature (). getName () + "Run ... @Before: The parameter list is: {"+ Arrays.asList (args) +"} " ); } // Post notification (@After): run after the end of the target method (div) (regardless of whether the method ends normally or is abnormal) End) @After (
  
"com.atneusoft.springboot.aop.LogAspects.pointCut ()" ) public void logEnd (JoinPoint joinPoint) { System.out.println ( "" + joinPoint.getSignature (). GetName () + "End ... @ After" ); } // JoinPoint must appear in the first position of the parameter table
// return notification (@AfterReturning ): Run after the target method (div) returns normally
@AfterReturning (value = "pointCut ()", returning = "result" ) public void logReturn (JoinPoint joinPoint, Object result) { System.out.println ( "" + joinPoint .getSignature (). getName () + "Return normally ... @ AfterReturning: Run result: {" + result + "}" ); } // Exception notification (@AfterThrowing): Run after the target method (div) has an exception @AfterThrowing (value = "pointCut ()", throwing = "exception" ) public void logException (JoinPoint joinPoint,Exception exception){ System.out.println ( "" + joinPoint.getSignature (). GetName () + "Exception ... Exception information: {" + exception + "}" ); } }

③Add both the aspect class and the business logic class (the class where the target method is located) to the container, and add @EnableAspectJAutoProxy to the configuration class [Turn on the annotation-based aop mode, which is the same as the following form of the configuration file

    <!-Turn on the aspect function based on the annotation 
    version- > <aop: aspectj-autoproxy> </ aop: aspectj-autoproxy>

 

@EnableAspectJAutoProxy 
@Configuration
public classMainConfigOfAOP {
     
    //Business logic class is added to the container
    @Bean
    publicMathCalculator calculator () {
        return newMathCalculator ();
    }

    //Cut face class is added to the container
    @ Bean
    publicLogAspects logAspects () {
        return newLogAspects ();
    }
}

to sum up

Three steps:
     1), add business logic components and aspect classes to the container; tell Spring which is the aspect class (@Aspect)
          2), mark a notification annotation on each notification method on the aspect class, tell Spring when Where to run (pointcut expression)
          3), enable annotation-based aop mode; @EnableAspectJAutoProxy

 

Guess you like

Origin www.cnblogs.com/tombky/p/12695008.html