Spring Boot custom annotations implement AOP

1. Custom annotation rules

  1. The Annotation type is defined as @interface. All Annotations will automatically inherit the java.lang.Annotation interface and cannot inherit other classes or interfaces.
  2. Parameter members can only be modified with public or default access rights.
  3. Parameter members can only use the eight basic data types of byte, short, char, int, long, float, double, boolean and data types such as String, Enum, Class, annotations, and arrays of these types.
  4. To obtain the annotation information of class methods and fields, you must obtain the Annotation object through Java's reflection technology, because you have no other way to obtain the annotation object.
  5. Annotations can also have no defined members, but then the annotations are useless.

2. Introduction to related annotations when defining custom annotations

@Documented annotation function: annotations that specify modifications can be documented by tools such as javadoc. They are only responsible for marking and have no member values.

@Retention annotation function: Indicates the life cycle of the modified annotation, that is, to which stage it will be retained. The values ​​of RetentionPolicy include the following three types:
SOURCE: retained at the source code level and discarded after compilation. CLASS: The compilation level is retained, exists in the compiled class file, and is discarded when the jvm is running. This is the default value.
RUNTIME: The run level is reserved, exists in the compiled class file, is reserved when the jvm is running, and can be called reflectively.

@Target annotation function: Indicates the scope of use of the modified annotation, that is, where the described annotation can be used. ElementType values ​​include the following:

TYPE: class, interface or enumeration FIELD: domain, including enumeration constants METHOD: method PARAMETER: parameter CONSTRUCTOR: constructor method
LOCAL_VARIABLE: local variable ANNOTATION_TYPE: annotation type PACKAGE: package

3. Use Spring’s AOP to first add Maven dependencies

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-aop</artifactId>
</dependency>

4. First create a self-defined annotation

@Documented
@Retention(RetentionPolicy.CLASS)
@Target(ElementType.METHOD)
public @interface MyAnnotation {
    
    
}

5.AOP configuration

@Aspect//Aspect
@Pointcut//Define where aspects are required
@annotation//Take effect when the executed method has the specified annotation.
@After
@Before
@Around

@Component
@Aspect
public class MyAnnotationAspect {
    
    
    @Pointcut("@annotation(com.example.demo.annotation.MyAnnotation)")
    private void myAnnotation() {
    
    
    }

    /**
     * 环绕通知
     */
    @Around("myAnnotation()")
    public void advice(ProceedingJoinPoint joinPoint) throws Throwable {
    
    
        System.out.println("around begin...");
        //执行到这里走原来的方法
        joinPoint.proceed();
        System.out.println("around after....");
    }

    @Before("myAnnotation()")
    public void record(JoinPoint joinPoint) {
    
    
        System.out.println("Before");
    }

    @After("myAnnotation()")
    public void after() {
    
    
        System.out.println("After");
    }
}

ProceedingJoinPoint and JoinPoint description
AspectJ uses the org.aspectj.lang.JoinPoint interface to represent the target class connection point object. If it is surround enhancement, use org.aspectj.lang.ProceedingJoinPoint to represent the connection point object. This class is a sub-interface of JoinPoint. Any enhancement method can access the information of the join point context by declaring the first input parameter as JoinPoint. Let’s first take a look at the main methods of these two interfaces:

1.JoinPoint
java.lang.Object[] getArgs(): Get the input parameter list when the join point method is running;
Signature getSignature(): Get the method signature object of the join point;
java.lang.Object getTarget(): Get the join point The target object where it is located;
java.lang.Object getThis(): Get the proxy object itself;
2.ProceedingJoinPoint
ProceedingJoinPoint inherits the JoinPoint sub-interface, which adds two new methods for executing the join point method:
java.lang.Object proceed( ) throws java.lang.Throwable: Execute the method at the connection point of the target object through reflection;
java.lang.Object proceed(java.lang.Object[] args) throws java.lang.Throwable: Execute the connection point of the target object through reflection The same method as above, but using new input parameters to replace the original input parameters.

6.Controller test

@RestController
public class MyAnnotationController {
    
    

    @MyAnnotation
    @RequestMapping("/aa")
    public void testAnnotation(){
    
    
        System.out.println("我正在测试自定义注解。。。");
    }
}

Insert image description here

Guess you like

Origin blog.csdn.net/weixin_45163291/article/details/128657981