Spring's meta-annotations

1. Introduction to meta-annotations 

1.1. Source code introduction 

 1.2. Meta annotation introduction

        As can be seen from the above picture, Spring has four meta-annotations [ responsible for annotating other annotations ], which are:

        @Target : Identifies which program elements the annotation can be used to mark, such as classes, methods, fields, etc.

        @Retention : Identify the life cycle of the annotation, that is, when the annotation will be retained. The optional life cycle includes source code (SOURCE), compile time (CLASS) and runtime (RUNTIME).

                RetentionPolicy.SOURCE: This annotation only exists in the source code, and the compiler will ignore these annotations when compiling. This means that these annotations will not be included in the compiled .class file. This annotation is generally used for compile-time inspection and debugging.

RetentionPolicy.CLASS: This annotation will be retained at compile time and stored in the .class file, but will be ignored at runtime. This annotation is mainly used for bytecode processing at compile time.

                RetentionPolicy.RUNTIME: The annotation will be retained at runtime, and the annotation information can be obtained through reflection. This kind of annotation is generally used to process some logic at runtime, such as implementing a custom IOC framework, AOP framework, etc.

                Generally speaking, if you need to obtain annotation information through reflection at runtime, you need to set the annotation's Retention property to RetentionPolicy.RUNTIME.

        @Documented : Identifies that the annotation will be included in JavaDoc.

        @Inherited : It is used to specify whether the annotation can be inherited. By default, the annotation will not be inherited by subclasses.

 2. The necessity of meta-annotations

        In the Spring Framework, the use of meta-annotations is optional, and some of them can be omitted as needed . Here are some meta-annotations that can be omitted:

        1. @Target: If the meta-annotation is not specified, the annotation can be applied to all target element types.

        2. @Retention: If this meta-annotation is not specified, the default life cycle is runtime (RUNTIME).

        3. @Documented: If the meta-annotation is not specified, the annotation will not be included in the Java document.

        4. @Inherited: If this meta-annotation is not specified, the annotation cannot be inherited by subclasses.  

3. Restrictions on omitting meta-annotations 

         Although from the above introduction, it seems that each of them can be omitted, but in fact, it is impossible to omit all four meta-annotations at the same time .

        Among them, @Retention and @Target are required because they define the life cycle and application target of the annotation. If these two meta-annotations are omitted, the compiler will not be able to parse and process the annotations correctly.

         @Documented and @Inherited are optional and can be omitted if desired. Note, however, that omitting @Documented causes the annotation not to appear in the Java documentation, while omitting @Inherited means the annotation cannot be inherited by subclasses.

Guess you like

Origin blog.csdn.net/weixin_52255395/article/details/131943369