The use of annotations (Annotation) in Java

annotation:

Mark the classes, methods, and member variables in Java, and then perform special processing.

Custom annotations:

special:

  • value attribute, if there is only one value attribute, the value name can be omitted when using the value attribute.
  • But if there are multiple attributes,   and multiple attributes have no default value, then the value name cannot be omitted.

Meta annotations:

Annotation Annotation Annotation

There are two meta-annotations:

 @Target : constrains where custom annotations can only be used, common values ​​are as follows

  • TYPE, class, interface
  • FIELD, member variable
  • METHOD, member method
  • PARAMETER, method parameter
  • CONSTRUCTOR, constructor
  • LOCAL_VARIABLE, local variable

 @ Retention : Declare the life cycle of annotations, common values ​​are as follows

  • SOURCE: Annotations only apply to the source code stage, and do not exist in the generated bytecode file
  • CLASS: The annotation works in the source code stage, the bytecode file stage, does not exist in the runtime stage, and the default value.
  • RUNTIME: annotations are used in the source code phase, bytecode file phase, and runtime phase (commonly used in development)

eg. stipulates that annotations can only be used in methods

Annotation analysis

  • Annotation: the top-level interface of annotations, all annotations are objects of type Annotation
  • AnnotatedElement: This interface defines the parsing methods related to annotation parsing
  • All class components Class, Method, Field, and Constructor implement the AnnotatedElement interface and they all have the ability to parse annotations

 

 

 

Guess you like

Origin blog.csdn.net/candice5566/article/details/125752224