Java study notes (AOP for aspect programming)

Notes on the Four Elements

1.@Target

Indicates where the annotation is used
public enum ElementType { TYPE, class, interface (including annotation type) or enum declaration FIELD, domain declaration (including enum instance) METHOD, method declaration PARAMETER, parameter declaration CONSTRUCTOR, constructor declaration LOCAL_VARIABLE, local variable Declare ANNOTATION_TYPE, Annotation type declaration PACKAGE, package declaration TYPE_PARAMETER, type parameter declaration TYPE_USE type usage }










2.@Retention

Indicates the range in which the annotation can be saved
public enum RetentionPolicy { SOURCE, source code: This annotation can only be saved in the source code. When compiled, it will be discarded. CLASS, class file: This annotation can be retained in the class file but will be jvm discards the RUNTIME runtime: that is, this annotation can be retained at runtime and can be obtained through reflection and reflection }



3.@Documented

That is, elements with this annotation can be documented by tools such as javadoc. It means that this annotation will be extracted into a document by the javadoc tool. The content in the doc document will be different due to the different information content of this annotation. Quite similar to @return, @param, etc.

4.@Inherited

Allow subclasses to inherit the annotations in the parent class. That is, the child class of the element with this annotation can inherit the annotation of the parent class.

Interceptor implementation demo

@Component //Let the IOC container manage
@Aspect //Define the aspect class
@Order(0) //Set the priority, the lower the value, the higher the priority
public class TestAspect {

/**
 * 系统状态处理切入点
 */
@Pointcut("execution(* com.test.aop.service.*.*(..))")
public void testPointCut() {
}

@Before("testPointCut()")
public void before(JoinPoint joinPoint) {
    //方法名
    MethodSignature methodSignature = (MethodSignature)  joinPoint.getSignature();
    //参数名称
    String[] params =  methodSignature.getParameterNames();
    //参数值
    Object[] args = joinPoint.getArgs();
    //获得注解信息
    LogAnnotation logAnnotation = methodSignature.getMethod().getDeclaredAnnotation(LogAnnotation.class);
    for (int i = 0 ; i <params.length; i++) {
        System.out.println(params[i]+ " = "+ args[i]);
    }
}

@After("testPointCut()")
public void  after (JoinPoint joinPoint) {
}

@AfterReturning(pointcut = "testPointCut()",returning = "returning")
public Object   afterReturn(JoinPoint joinPoint, Object returning) {
}
/**
 * 系统状态处理
 * 
 * @param point 处理加入点
 * @return 返回结果
 */
@Around("testPointCut()")
public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
    String classPath = joinPoint.getTarget().getClass().getName();
    String methodName =  joinPoint.getSignature().getName();
    //获取参数名称、参数值
    MethodSignature methodSignature = (MethodSignature) joinPoint.getSignature();
    //取得参数
    String[] params = methodSignature.getParameterNames();
    //参数值
    Object[] args = joinPoint.getArgs();
    for (int i = 0; i < params.length; i++) {
        System.out.println(params[i]+" = "+args[i]);
    }
    //取注解
    LogAnnotation logAnnotation = methodSignature.getMethod().getDeclaredAnnotation(LogAnnotation.class);
    System.out.println("value= "+logAnnotation.value()+",des ="+logAnnotation.description());
    Object result = result =joinPoint.proceed(args);
    return  result;
}
//异常执行
@AfterThrowing(pointcut = "testPointCut()",throwing = "err")
public void throwing(Throwable err) {
}

}
Aspect execution flow chart
The function
of @Around surround notification is the most powerful notification in SpringAOP. It can implement pre-notification and post-notification at the same time. It retains the original function of scheduling the proxy object, so it is both powerful and flexible, but The controllability is not strong, if you don't need a lot of business logic changes, generally speaking, you don't need to use it.

Guess you like

Origin blog.csdn.net/cyb_123/article/details/107510671