Handwritten Spring Framework - Prelude - Annotations and Custom Annotations

Table of contents

annotation

introduce

Function

Classification

Annotation processor class library

custom annotation

Common Meta Annotations

customize


  • annotation

  • introduce

  • Provides a way to set metadata for program elements
  • Used to associate any information or metadata (metadata) with program elements (classes, methods, member variables, etc.)
  • Metadata refers to the data of the data, that is, the description of the data
    • Metadata is extra information added to program elements such as methods, fields, classes, and packages
    • Annotation is a decentralized metadata setting method, and XML is a centralized setting method
    • Annotations cannot directly interfere with the execution of program code
  • In layman's terms, annotation is to save the required information and call it when we need it (compile time, runtime)
  • Annotation is essentially a special interface that inherits Annotation
  • Its concrete implementation class is a dynamic proxy class generated by the Java runtime
  • Function

  • As a specific tag, used to tell the compiler some information
  • Dynamic processing at compile time, such as dynamically generating code (for example, Lombok's @Data generates the get... method of the entity class)
  • Dynamic processing at runtime, as a carrier of additional information, such as obtaining annotation information (writing path and other information into the annotation class to obtain)
  • Classification

  • Standard Notes:
    • Override, Deprecated (not encouraged to use, no longer maintained), SuppressWarnings (need to ignore something)
  • Meta annotations:
    • @Retention (life cycle), @Target (action target), @Inherited, @Documented
  • Meta-annotations are annotations used to modify annotations, usually used in the definition of annotations

  • All meta-annotations are defined under the java.lang.annotation package
  • Among them, Annotation is the basic interface of annotations, and all annotations inherit this interface

  • Annotation processor class library

  • AnnotatedElement interface is the parent interface of all program elements (Class, Method and Constructor)
  • So after the program obtains the AnnotatedElement object of a certain class through reflection, the program can call the following method of the object to access the Annotation information:
  • 方法1:<T extends Annotation> T getAnnotation(Class<T> annotationClass):
    • Returns the annotation of the specified type that exists on this program element, or null if the type annotation does not exist
  • Method 2: Annotation[] getAnnotations():
    • Returns all annotations present on this program element
  • 方法3:boolean isAnnotationPresent(Class<?extends Annotation> annotationClass):
    • Determine whether the program element contains the annotation of the specified type, if it exists, return true, otherwise return false
  • 方法4:Annotation[] getDeclaredAnnotations(Class annotationClass):
    • Returns all annotations that exist directly on this element; unlike other methods in this interface, this method ignores inherited annotations (or returns an array of length zero if no annotations exist directly on this element) The method's The caller is free to modify the returned array; this has no effect on arrays returned by other callers

  • custom annotation

  • Common Meta Annotations

  • java.lang.annotation provides four meta-annotations:
  • Specially annotate other annotations (when customizing annotations, you need to use meta-annotations)
  • 1---@Documented: Whether the annotation should be included in the JavaDoc document
  • 2---@Retention: Specify the life cycle of the annotation (source code, class file, runtime)
    • RetentionPolicy.SOURCE : Discard at compile time
    • These annotations have no meaning after compilation, so they are not written into the bytecode
    • That is, annotations can only be retained in the original file, and will be eliminated in the compiled class file
    • @Override, @SuppressWarnings belong to this type of annotation
    • RetentionPolicy.CLASS : Discard when class is loaded
    • Useful in the processing of bytecode files
    • That is, annotations will not only be retained in the original file, but also in the compiled class file
    • Annotations use this method by default
    • RetentionPolicy.RUNTIME : It will never be discarded, and the annotation is also retained during runtime, so the information of the annotation can be read using the reflection mechanism
    • That is, the information of the annotation is obtained at runtime, and it is valid at runtime
    • At the same time, you can also get other useful information in the annotation through reflection
    • Custom annotations usually use this method
  • 3---@Target: Specify the target scope (class, method, field, etc.) used by the annotation
    • ElementType.CONSTRUCTOR: used to describe the constructor
    • ElementType.FIELD: member variables, objects, attributes (including enum instances)
    • ElementType.LOCAL_VARIABLE: Used to describe local variables
    • ElementType.METHOD: used to describe the method
    • ElementType.PACKAGE : used to describe the package
    • ElementType.PARAMETER: used to describe parameters
    • ElementType.ANNOTATION_TYPE: applied to an annotation type
    • ElementType.TYPE: used to describe classes, interfaces (including annotation types) or enum declarations
  • 4---@Inherited: Whether to allow subclasses to inherit the annotation (subclasses modified by this annotation can automatically inherit the annotations modified by the parent class annotated @Inherited) can only be annotations on the class, annotations on methods and fields cannot be inherited
  • customize

  • Annotation type defined as @interface
  • All Annotation will automatically inherit the java.lang.Annotation interface, and can no longer inherit other classes or interfaces
  • Parameter members can only be modified with public or default (default) access rights

  • Parameter members can only use eight basic data types and data types such as String, Enum, Class, annotations, and arrays of these types

  • To obtain the annotation information of class methods and fields, the Annotation object must be obtained through Java reflection technology

Guess you like

Origin blog.csdn.net/weixin_59624686/article/details/130174192