The definition and use of java annotation easy to understand tutorial series one

1.java annotation definition

Definition : Annotation (annotation) is that Java provides a way and method for elements in a metaprogram to associate any information with any metadata. Annotion is an interface. The program can obtain the Annotion object of the specified program element through reflection, and then obtain the metadata in the annotation through the Annotion object.

To put it simply : Java Annotation, also known as Java annotation , is an annotation mechanism introduced by JDK5.0.

In layman's terms : Java annotations (Annotation) 1. What you can do : Save the corresponding content according to the attribute field, which can be placed in the class, method and other places. 2. Information that can be obtained : the content saved in the attribute field and the place (class, method, etc.). A few analogy examples: Annotations are signs, road signs, and lighthouses, which can provide some information but do not take the initiative to do anything .

Question: Students who have used this place but do not understand the principle will have questions: According to this explanation, the annotation is a label to save information, only showing some information, and doing nothing. That is not the same as the usual annotations such as @SpringBootApplication, @MapperScan, @RestController, etc., because these annotations definitely not only display information, but also do some processing logic . Where is this logic written?

Answer : Therefore, to write a custom annotation, you need to specify and know the location of the annotation in advance , so that it can take effect correctly. In the specified position, determine if this custom annotation is used, and use the information provided by the annotation attribute to logically process the position where the annotation is used through AOP and reflection . (Not only using aop, as long as it can be captured and processed before the annotation is executed)

2. How to use annotations

(1) For example: use @RequestMapping to display

(2) Check the annotation source code of @RequestMapping, see where the annotation can be used @Target, and the life cycle of the annotation: @Retention

@Target({ElementType.METHOD, ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Mapping  是另外一个自定义注解和这个组合了,在此不做讨论,有兴趣看后面教程

(3) The top four annotations are used to write the usage rules of this annotation. The use position @Target({ElementType.METHOD, ElementType.TYPE} is: used METHOD on [method], TYPE on [class, interface, annotation] use annotations specific Detailed follows:

(4) Among them, the target also introduced two attributes in java 1.8.

  • TYPE_PARAMETER Introduced using Java 1.8 on [type parameters]
  • TYPE_USE Use Java 1.8 to introduce in [any place where the type is declared]

(5) Then check how to use the attribute defined by the annotation, such as the name attribute here, you need to check what logic is executed before and after calling this annotation.

(6) What logic is implemented for how to use a specific annotation, two aspects: 1. Document explanation (this is easier for novices) 2. By viewing the source code where the annotation is referenced, and what business logic is implemented (this It’s difficult for those who are just getting started). The eclipse shortcut I use is ctrl+G on the annotation or right click on the annotation Find Usages to view

Summary : The use of annotations is directly @ annotation names on specific methods, classes, etc., which is very simple. Check the usage location defined by @target before use, and check the usage logic of annotations through the source code and documentation .

Guess you like

Origin blog.csdn.net/Mint6/article/details/103831476