annotations
1. Yuan note
Meta-annotations are the annotations that define annotations and are the basic annotations provided by Java for defining annotations
annotations | instructions |
---|---|
@Retention | Annotation Class, implement declaration Class, declaration Class, declaration extension |
@Target | Place it above a custom annotation to indicate the extent to which the annotation can be used |
@Inherited | Allows a subclass to inherit comments from its parent, and in a subclass you can get comments that use the parent class |
@Documented | Indicates that the annotation is recorded by Javadoc |
@interface | Use a type from the defined annotation |
1.1 @ Target
The purpose of this annotation is to tell Java where to put custom annotations, such as classes, methods, constructors, variables, etc.
Its value is an enumerated type with the following attribute values
- Elementtype. CONSTRUCTOR: Used to describe the CONSTRUCTOR
- Elementtype. FIELD: Used to describe member variables, objects, and attributes
- Elementtype. LOCAL_VARIABLE: Used to describe local variables
- Elementtype. METHOD: Used to describe methods
- Elementtype. PACKAGE: Used to describe packages
- Elementtype. PARAMETER: Used to describe parameters
- Elementtype. TYPE: Used to describe a class, interface, or enum declaration
1.2 @ Retention
This annotation is used to explain the life cycle of a custom annotation, and there are three life cycles in the annotation
- Retentionpolicy. RUNTIME: The annotation is never discarded, and is retained at RUNTIME. It can be read using reflection
- Retentionpolicy. CLASS: discarded when the CLASS is loaded. This is used by default
- Retentionpolicy. SOURCE: discarded at compile time, custom annotations are meaningless after compile, so they are not written into bytecode, familiar
@Override falls into this category
1.3 @ Inherited
The annotation is a tag annotation, indicating that the marked type can be inherited. If an annotation using this tag is used, it is also used by a subclass of that class
1.4 @ Documented
This annotation indicates whether the annotation information is added to the Java document
1.5 @ interface
This annotation is used to declare an annotation, in which each method actually declares a configuration parameter, the method name is the parameter name, and the return value type is the parameter type
2. Implement a custom annotation
Start by creating a custom annotation
package com.example.demo;
import org.springframework.stereotype.Component;
import java.lang.annotation.*;
@Target({ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Component
public @interface MyAnnotation {
String value(a);
}
Copy the code
Then write a business logic that intercepts the custom annotation method and prints the annotation parameters on the console
package com.example.demo;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;
import java.lang.reflect.Method;
@Aspect
@Component
public class TestAnnotationAspect {
@Pointcut("@annotation(com.example.demo.MyAnnotation)")
public void myAnnotationPointCut(a){}
@Before("myAnnotationPointCut()")
public void before(JoinPoint joinPoint)throws Throwable{
MethodSignature sign=(MethodSignature) joinPoint.getSignature();
Method method=sign.getMethod();
MyAnnotation annotation=method.getAnnotation(MyAnnotation.class);
System.out.println("Test Annotation parameter:"+annotation.value()); }}Copy the code
Finally, implement a Controller that checks to see if the business logic is printed when the page is visited
package com.example.demo;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class TestAnnotationController {
@GetMapping("/test")
@myAnnotation (" Test custom annotations ")
public String test(a){
return "shelgi"; }}Copy the code
The result is as follows