Detailed java custom annotation case

There are three categories of annotations: 

Annotation is essentially an interface that inherits Annotation

The first type is jdk predefined annotations

        * @Override: Check whether the method marked by the annotation is inherited from the parent class (interface)
        * @Deprecated: The content marked by the annotation indicates that it is out of date
        * @SuppressWarnings: Suppress warnings
            * Generally pass the parameter all @SuppressWarnings("all ")

The second type is meta annotation

            * @Target: describes the position where the annotation can be used
                * ElementType value:
                    * TYPE: can be used on the class
                    * METHOD: can be used on the method
                    * FIELD: can be used on the member variable
            * @Retention: describes the stage where the annotation is retained
                * @Retention(RetentionPolicy.RUNTIME): The currently described annotation will be retained in the class bytecode file and read by JVM
            * @Documented: Describe whether the annotation is extracted into the api document
            * @Inherited: Describe the annotation Whether it is inherited by subclasses

The third type is custom annotation

3.1 Custom annotation can define the attribute list (abstract method)

                1. The return value type of the property has the following values
                    * Basic data type
                    * String
                    * Enumeration
                    * Note
                    * The above type of array

                2. The property is defined, and the property needs to be assigned
                    when using it. 1. If the property is defined, use the default key The word gives the attribute the default initial value, when using the annotation, the attribute assignment is not required.
                    2. If only one attribute needs to be assigned, and the name of the attribute is value, the value can be omitted and the value can be defined directly.
                    3. When assigning an array, the value is wrapped in {}. If there is only one value in the array, {} can be omitted

3.2 Custom annotation case

3.3 The principle of obtaining attribute values ​​from custom annotations

            * getAnnotation(Class)
            //In fact, a subclass implementation object of the annotation interface is generated in memory

                    public class ProImpl implements Pro{                         public String className(){                             return "[Define attribute value]";                         }                         }                     }





Output result:

 

 

 

Guess you like

Origin blog.csdn.net/Growing_hacker/article/details/109093040