Meta annotations in java (simple and easy to understand)

Meta-annotations in Java are annotations used to annotate other annotations.

Java provides four meta-annotations, namely: @Retention, @Target, @Documented and @Inherited.

  1. @Retention: Used to specify the retention time of the annotation annotated by it. There are three values: RetentionPolicy.SOURCE indicates that the annotation is only retained in the source code and will not be retained after compilation; RetentionPolicy.CLASS indicates that the annotation is retained at compile time, but will not be loaded into the JVM at runtime; RetentionPolicy.RUNTIME indicates that Annotations are preserved at runtime and can be read via reflection.

  2. @Target: Used to specify where the annotations annotated by it can be applied. Commonly used values ​​are

    • ElementType.TYPE(class, interface, enumeration),

    • ElementType.FIELD(field),

    • ElementType.METHOD (method),

    • ElementType.PARAMETER(parameter) etc.

  3. @Documented: Used to specify whether the annotation annotated by it will be included in the JavaDoc document.

  4. @Inherited: Used to specify whether the annotation annotated by it can be inherited by subclasses. If an annotation annotated by @Inherited is applied to a class, and the subclass of this class does not apply any annotations, the subclass will inherit the annotation of the parent class.

These meta-annotations can help developers define their own annotations more flexibly, and control the behavior and scope of annotations.

Guess you like

Origin blog.csdn.net/qq_64680177/article/details/131868623