Spring MethodInterceptor-- easy aop function in three ways

Next is laying the groundwork to understand the track log link under AOP, this one I do not have to re-write this one can look at a basic
text link: https: //blog.csdn.net/u013905744/article/details/91364736

If you are unsure or spring aop for spring MethodInterceptor concept, reference: spring aop, the Spring Interceptor, Interceptor SpringMVC What is the difference?

The following are performed in the spring boot environment

There are two ways to set the AOP (implemented weaving Weave) under spring boot:

1. @Aspect comment

2. 使用DefaultPointcutAdvisor

For example in order to achieve TracingInterceptor

Method 1: Use aspectj execution (the cut point) + interceptor (Enhanced Advice) constituting weaving (DefaultPointcutAdvisor)

interceptor


  
  
  1. class TracingInterceptor implements MethodInterceptor {
  2. Object invoke(MethodInvocation i) throws Throwable {
  3. System.out.println( "method "+i.getMethod()+ " is called on "+
  4. i.getThis()+ " with args "+i.getArguments());
  5. Object ret=i.proceed();
  6. System.out.println( "method "+i.getMethod()+ " returns "+ret);
  7. Return the right;
  8. }
  9. }

Weaving configuration class

 


  
  
  1. @Configuration
  2. public class InterceptorConfig {
  3. public static final String traceExecution = "execution(* com.hfi.aop..*.*(..))";
  4. @Bean
  5. public DefaultPointcutAdvisor defaultPointcutAdvisor2() {
  6. TracingInterceptor interceptor = new TracingInterceptor();
  7. AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
  8. pointcut.setExpression(traceExecution);
  9. // configuration enhancements class advisor
  10. DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
  11. advisor.setPointcut(pointcut);
  12. advisor.setAdvice(interceptor);
  13. return advisor;
  14. }
  15. }

Effect: When the execution method in a packet to com.hfi.aop, when performing the method performEncore

We can see the configuration of the entry into force of TracingInterceptor

Method 2: Use custom annotations (cut point) + interceptor (Enhanced Advice) constituting weaving (DefaultPointcutAdvisor)

Custom annotation HfiTrace


  
  
  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface HfiTrace {
  5. }

interceptor


  
  
  1. public class TracingInterceptor implements MethodInterceptor {
  2. @Override
  3. public Object invoke(MethodInvocation invocation) throws Throwable {
  4. Method method = invocation.getMethod();
  5. HfiTrace annotation = getAnnotation(method);
  6. if (annotation == null) {
  7. return invocation.proceed();
  8. }
  9. // Why do twice the output when http://127.0.0.1:8089/jpademo/perform call?
  10. // Because Audience inside using a @Around, will be intercepted twice
  11. System.out.println( "method " + invocation.getMethod() + " is called on " + invocation.getThis() + " with args" +
  12. " " + invocation.getArguments());
  13. Object proceed = invocation.proceed();
  14. System.out.println( "method " + invocation.getMethod() + " returns " + proceed);
  15. return proceed;
  16. }
  17. private HfiTrace getAnnotation(Method method) {
  18. // If there are multiple annotation seems not easy to use as on the controller has been because the @RequestMapping annotated so ...
  19. if (method.isAnnotationPresent(HfiTrace.class)) {
  20. return method.getAnnotation(HfiTrace.class);
  21. }
  22. return null;
  23. }
  24. }

Weaving configuration class


  
  
  1. @Configuration
  2. public class InterceptorAnnotationConfig {
  3. @Bean
  4. public DefaultPointcutAdvisor defaultPointcutAdvisor3() {
  5. TracingInterceptor interceptor = new TracingInterceptor();
  6. // AnnotationMatchingPointcut pointcut = new AnnotationMatchingPointcut(HfiTrace.class, true);
  7. JdkRegexpMethodPointcut pointcut = new JdkRegexpMethodPointcut();
  8. pointcut.setPatterns( "com.hfi.*");
  9. // configuration enhancements class advisor
  10. DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor();
  11. advisor.setPointcut(pointcut);
  12. advisor.setAdvice(interceptor);
  13. return advisor;
  14. }
  15. }

Business code


  
  
  1. @HfiTrace
  2. @Override
  3. public String perform() {
  4. System.out.println( "perform...");
  5. return "perform";
  6. }

effect:

We can also see the implementation of the entry into force of

Method 3: Use custom annotations (cut point) + @ Aspect (section) configured weaving

Custom annotation HfiTrace


  
  
  1. @Target(ElementType.METHOD)
  2. @Retention(RetentionPolicy.RUNTIME)
  3. @Documented
  4. public @interface HfiTrace {
  5. String name () default "default annotation information" ;
  6. }

Weaving configuration class


  
  
  1. @Component
  2. @Aspect
  3. public class TracingAspect {
  4. @Before( "@annotation(test)")
  5. public void beforeTest(JoinPoint point, HfiTrace test){
  6. System.out.println( "method " + point.getSignature().getName() + " is called on " + point.getThis() + " with " +
  7. "args" +
  8. " " + point.getArgs());
  9. System.out.println( "before invoke: "+ test.name());
  10. }
  11. @AfterReturning(value = "@annotation(test)", returning = "rvt")
  12. public void afterTest(JoinPoint point, HfiTrace test, Object rvt) {
  13. System.out.println( "method "+point.getSignature().getName() + " returns " + rvt);
  14. System.out.println( "after invoke: " + test.name());
  15. }
  16. }

Business code:

controller layer:


  
  
  1. @HfiTrace
  2. @GetMapping( "/perform")
  3. public String perform() {
  4. String perform = performance.perform();
  5. return perform;
  6. }
  7. @HfiTrace(name = "abc")
  8. @GetMapping( "/performEncore")
  9. public String performEncore() {
  10. // cast
  11. Encoreable encoreable = (Encoreable) performance;
  12. return encoreable.performEncore();
  13. }

service layer:


  
  
  1. @Component
  2. public class PerformanceImpl implements Performance {
  3. @HfiTrace
  4. @Override
  5. public String perform() {
  6. System.out.println( "perform...");
  7. return "perform";
  8. }
  9. }

effect:

To sum up: three ways can achieve the same functionality, Method 3 seems the most simple, only need to define a comment, and then write a @Aspect cut class, you can run the specified method of the intercept

Published 18 original articles · won praise 45 · views 110 000 +

Guess you like

Origin blog.csdn.net/zhibo_lv/article/details/104893154