Spring Aop--notification annotations

1. Surround annotations

Surround annotation

Surround annotation @Aroud

annotation describe
@Around @Around is a notification type in Spring AOP, used to perform surrounding operations before and after the target method is executed. It can add additional logic before and after method calls, such as logging, performance monitoring, etc. The @Around annotation needs to be matched with the AspectJ expression to specify the entry point, define the method of the aspect class, and control the execution process of the target method through the ProceedingJoinPoint parameter inside the method.

Surround annotations enable the integration of enhancements.

For surrounding annotations, you need to create a ProceedingJoinPoint object and use two corresponding methods.

method describe
proceed() Execute the notified target method and return its return value. This method must be called in a surrounding advice, otherwise the target method will not execute.
getArgs() Get the parameter value array of the target method. The returned parameter array is of type Object.

Surround annotation use cases:

@Component
@Aspect
public class RoundAdvice {

    @Around("com.alphamilk.Advice.MyPointcut.pointcut1()")
    public Object Transation(ProceedingJoinPoint joinPoint){
//        获取方法参数
        Object[] args = joinPoint.getArgs();
//        获取返回值
        Object result = null;

        try {
            System.out.println("事务开始");
//            执行对应方法
           result =  joinPoint.proceed(args);
            System.out.println("事务结束");
        } catch (Throwable e) {
            System.out.println("事务回滚");
            throw new RuntimeException(e);
        }
        return result;
    }
}

 Note: You need to enable notification annotations to use enhancements

@ComponentScan(value = "com.alphamilk")
@Configuration
//开启增强注解
@EnableAspectJAutoProxy
public class JavaConfig {
}

 If you do not use surrounding annotations, you need to use @Before @After @AfterReturning, etc.

@Component
@Aspect
public class advice {
  @Before("com.alphamilk.Advice.MyPointcut.pointcut1()")
    public void Before(JoinPoint joinPoint) {
        System.out.println("事务开始");
    }

 @After("com.alphamilk.Advice.MyPointcut.pointcut1()")
    public void After(JoinPoint joinPoint) {
        System.out.println("事务结束");
    }

    @AfterReturning(value = "com.alphamilk.Advice.MyPointcut.pointcut1()",returning = "result")
    public void AfterReturning(JoinPoint joinPoint,Object result) {
        System.out.println("调用拥有返回值的方法");
    }

    @AfterThrowing(value = "com.alphamilk.Advice.MyPointcut.pointcut1()",throwing = "throwable")
    public void AfterThrowing(JoinPoint joinPoint,Throwable throwable) {
        System.out.println("调用有异常的方法");
    }
}

Advantages and Disadvantages of Using Surround Annotations

advantage:

  1. High flexibility: Surround annotations provide the greatest degree of flexibility. Additional logic code can be inserted before and after the execution of the target method to fully control the execution process of the method.
  2. Unified processing: Through surrounding annotations, common logic codes can be extracted into aspects to achieve unified processing logic and avoid repeatedly writing the same code in each target method.
  3. The return value can be modified: In surrounding advice, the final result can be affected by modifying the return value of the target method.

shortcoming:

  1. Increased complexity: Compared with other types of notifications, the use of surrounding annotations is slightly more complicated and requires more understanding and mastery, especially for the use of ProceedingJoinPoint.
  2. Performance overhead: Since the surrounding annotation will wrap the execution process of the entire target method, it may cause a certain performance overhead in some cases, especially when the processing logic is more complex.
  3. Possible introduction of side effects: When making any modifications to the target method in a surround notification, you need to operate with caution to avoid introducing unpredictable side effects, which may lead to abnormal or abnormal behavior of the program.

 2. Priority annotation

annotation describe
@Order @Order is an annotation in the Spring framework used to define the loading order of components. It can be used at class level or method level. When multiple components implement the same interface or inherit the same parent class, their loading order can be specified through the @Order annotation. The smaller the value of @Order, the higher the priority and the higher the loading order. The value of the @Order annotation can be any integer. It should be noted that the loading order of components with the same priority is uncertain, so it is best to set the priorities to different values ​​to avoid uncertainty.

If there are two or more enhancements for the same method, and the order of enhancements needs to be specified, you need to use the @Order priority annotation to set it.

For its use, the core is: specify a priority. The lower the value of Order, the higher the priority. The higher the priority, the higher the priority, the front will be executed first, and the last will be executed.

Case code: (first enhancement)

@Component
@Aspect
//设置一个优先级更高注解
@Order(10)
public class advice {
  @Before("com.alphamilk.Advice.MyPointcut.pointcut1()")
    public void Before(JoinPoint joinPoint) {
        System.out.println("优先级高前置执行");
    }

 @After("com.alphamilk.Advice.MyPointcut.pointcut1()")
    public void After(JoinPoint joinPoint) {
        System.out.println("优先级高后置后执行");
    }

    @AfterReturning(value = "com.alphamilk.Advice.MyPointcut.pointcut1()",returning = "result")
    public void AfterReturning(JoinPoint joinPoint,Object result) {
        System.out.println("调用拥有返回值的方法");
    }

    @AfterThrowing(value = "com.alphamilk.Advice.MyPointcut.pointcut1()",throwing = "throwable")
    public void AfterThrowing(JoinPoint joinPoint,Throwable throwable) {
        System.out.println("调用有异常的方法");
    }
}

The second enhancement

@Component
@Aspect
@Order(20)
public class RoundAdvice {

    @Around("com.alphamilk.Advice.MyPointcut.pointcut1()")

    public Object Transation(ProceedingJoinPoint joinPoint){
        Object[] args = joinPoint.getArgs();
        Object result = null;

        try {
            System.out.println("优先级低前置后执行");
//            执行对应方法
           result =  joinPoint.proceed(args);
            System.out.println("优先级低后置先执行");
        } catch (Throwable e) {
            System.out.println("事务回滚");
            throw new RuntimeException(e);
        }
        return result;
    }
}


 

Guess you like

Origin blog.csdn.net/dogxixi/article/details/132617150