Detailed explanation of annotation (@Retention@Target)

1. Annotations: In-depth understanding of JAVA annotations

     To learn about annotations in depth, we must be able to define our own annotations and use them. Before defining our own annotations, we must understand the meta-annotations provided by Java and the syntax for defining annotations.

1. Meta-annotation:

The role of meta-annotations is to annotate other annotations. Java 5.0 defines four standard meta-annotation types, which are used to provide descriptions of other annotation types. Meta-annotations defined in Java 5.0:

1.@Target,

2.@Retention,

3.@Documented,

4.@Inherited

These types and the classes they support can be found in the java.lang.annotation package. Let's take a look at the role of each meta-annotation and the instructions for using the corresponding sub-parameters.

@Target:

@Target explains that Annotation can be used for packages, types (classes, interfaces, enumerations, Annotation types), type members (methods, constructors, member variables, enumeration values), method parameters and local variables (such as loop variables, catch parameter). The use of target in the declaration of the Annotation type makes it clearer what it is modifying.

Role: used to describe the scope of use of the annotation (ie: where the described annotation can be used)

The values ​​(ElementType) are:

1. CONSTRUCTOR: used to describe the constructor

2.FIELD: used to describe the domain

3.LOCAL_VARIABLE: used to describe local variables

4.METHOD: Used to describe the method

5.PACKAGE: used to describe the package

6.PARAMETER: used to describe parameters

7.TYPE: Used to describe classes, interfaces (including annotation types) or enum declarations

Example of use:

@Target(ElementType.TYPE)
public @interface Table {
    /**
     * Data table name annotation, the default value is the class name
     * @return
     */
    public String tableName() default "className";
}

@Target(ElementType.FIELD)
public @interface NoDBColumn {

}

Annotation Table can be used to annotate classes, interfaces (including annotation types) or enum declarations, while annotation NoDBColumn can only be used to annotate member variables of classes.

@Retention

   @Retention defines how long the Annotation is retained; some Annotations only appear in the source code and are discarded by the compiler; while others are compiled in the class file; Annotation compiled in the class file may be virtualized The machine ignores, and others will be read when the class is loaded (please note that it does not affect the execution of the class, because the Annotion and the class are used separately). Use this meta-Annotation to limit the "lifetime" of Annotation.

Function: Indicates at what level the annotation information needs to be saved to describe the life cycle of the annotation (ie: in what scope is the described annotation valid)

The values ​​(RetentionPoicy) are:

1. SOURCE: Valid in the source file (ie the source file remains)

2. CLASS: Valid in class files (ie class retention)

3. RUNTIME: Valid at runtime (that is, reserved at runtime)

The Retention meta-annotation type has a unique value as a member, and its value comes from the enumeration type value of java.lang.annotation.RetentionPolicy. Specific examples are as follows:

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325817762&siteId=291194637