28 API07 - notes, definitions and notes

1 Notes

Annotation is very powerful. It can enhance our java code, and use reflection technology to expand and realize many functions. They are widely used in the bottom three frameworks.
Traditionally, we declare through XML text file (as shown in the figure below, but XML is cumbersome and difficult to check), but now the most mainstream development is based on annotation, which has a small amount of code. The framework can automatically generate a lot of code according to the annotation, so as to reduce the amount of code and make the program easier to read. For example, the most popular SpringBoot is implemented entirely based on annotation technology.

The annotation design is very exquisite. When I first learn, I feel very alternative, even redundant, or even garbage. Why do you need @ annotation when you have java code? However, you will marvel at the fact that it can surpass the functions of Java code and make java code powerful in an instant. Take your time.

2 classification of notes

Notes are divided into three categories. Let's first understand:

  • JDK's own annotation
  • Meta annotation
  • Custom annotation

2.1 JDK notes

There are five JDK annotations:

@Override: used to identify the override method
@The Deprecated flag indicates that this method is outdated, but I will use it. Don't prompt me to expire
@SuppressWarnings("deprecation") ignore warnings
@SafeVarargs jdk1.7. Heap pollution occurs, which is not commonly used
@FunctionallInterface jdk1.8. It is not commonly used in conjunction with functional programming ramda expression

2.2 yuan notes

There are only five annotations used to describe annotations:

@Where are Target annotations used: on classes, methods, properties, and so on
@Life cycle of Retention annotation: in source file, bytecode file and running
@Inherited allows child annotation inheritance
@The javadoc generated by Documented contains annotations, which are not commonly used
@Repeatable annotation is a repeatable type annotation, which can be used multiple times in the same place and is not commonly used

2.2.1 @Target ElementType...

Describe where the annotation exists:

ElementType.TYPE applies to the elements of the class
ElementType.METHOD applied to method level
ElementType.FIELD applies to fields or attributes (member variables)
ElementType.ANNOTATION_TYPE applies to annotation types
ElementType.CONSTRUCTOR applies to constructors
ElementType.LOCAL_VARIABLE applies to local variables
ElementType.PACKAGE applies to package declarations
ElementType.PARAMETER applies to the parameters of the method

2.2.2 @Retention RetentionPolicy...

This annotation defines how long custom annotations are retained. For example, some annotations only appear in the source code and are discarded by the compiler; Others are compiled in class files; Annotations compiled in the class file may be ignored by the virtual machine, while others will be read when the class is loaded.

Why do you want to divide the bytecode file into two parts? If not, the reflection technology will not be available, so it will not be able to identify and process. There are three values:

SOURCE is valid in the SOURCE file (i.e. SOURCE file retention)
class is valid in the class file (i.e. class is reserved)
RUNTIME is valid at run time (i.e. reserved at run time)

3 user defined annotation

Note: the syntax of annotations is different from that of conventional java
Create package: CN tedu. annotation
Create class: testannotation java

package cn.tedu.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

/*This class is used to complete custom annotation*/
public class TestAnnotation {
}
//2. Mark the usage position of user-defined annotation through @ Target annotation
/*3.Specify where custom annotations can be used through meta annotation @ Target
* We use "ElementType. Static constant" to specify where the user-defined annotation can be added
* Moreover, the value can be written in multiple formats: @ Target({ElementType.XXX,ElementType.XXX}*/
@Target({ElementType.METHOD,ElementType.TYPE})//Can be added to methods & classes
//3. Mark the life cycle of user-defined annotation through @ Retention annotation
/*4.Customize the life cycle of annotations through meta annotation @ Retention rules
* We use "RetentionPolicy. Static constant" to specify the life cycle of custom annotations
* Note: only one value can be written: select 1 from SOURCE CLASS RUNTIME 3 */
@Retention(RetentionPolicy.RUNTIME)//Valid until runtime
//1. Define user-defined annotation
/*1.First of all, note that the syntax of annotation definition is different from that of Java
* 2.Define the format of custom annotation: @ interface annotation name*/
@interface Rice{
    //5. We can enhance the function of annotation -- add the attribute of annotation
    /*5.Note: int age(); Instead of defining a method, an age attribute is added to the custom annotation*/
    //int age();// Add a common attribute age to the custom annotation, and the type is int
    int age() default 0;//Assign a default value of 0 to the common properties of custom annotations
    /*6.A special attribute value can also be added to the annotation
    * Special attributes are defined in the same way as ordinary attributes, mainly in different ways
    * Note: the name of a special attribute must be value, but the type is not limited
    * Special attributes can also be given default values. The format is the same as that of ordinary attributes and cannot be abbreviated
    * */
    //String value();// Define a special attribute value of type string
    String value() default "Lemon";//Define special attributes and give them default values
}

//4. Define a class to test custom annotations
//@Rice
class TestAnno{
    /*Test 1: add Rice annotation to the name attribute and eat method of TestAnno class respectively
    * Conclusion: an error is reported in the annotation on the attribute, indicating where the user-defined annotation can be added, which is determined by @ Target*/
    //@Rice / / error reported
    String name;
    /*Test 2: when we add an age attribute to the Rice annotation, the @ Rice annotation directly reports an error when used
    * Conclusion: when the annotation does not define attributes, it can be used directly
    *      After the attribute is defined in the annotation, the attribute must be assigned a value. Format: @ Rice(age = 10)*/
    /*Test 3: after giving the default value to the age attribute, you can directly use the @ Rice annotation
    * There is no need to assign a value to the age attribute, because the age attribute already has a default value of 0*/
    /*Test 4: after adding a special attribute value to Rice annotation, the attribute must be assigned a value
    * However, special attribute assignment can be abbreviated as @ Rice("Apple")
    * Test 5: if a special attribute is also given a default value, you can use this annotation directly
    * If you want to assign values to all attributes of an annotation, each assignment cannot be abbreviated*/
    @Rice(age=10,value="orange")
    //@Rice("Apple")
    //@Rice(age = 10)
    //@Rice(10) / / an error is reported. It cannot be abbreviated. Ordinary attributes do not have this format
    public void eat(){
        System.out.println("I'm not active in cooking and have problems with my mind");
    }
}

Congratulations, you have passed another level. We will use annotations frequently in the follow-up framework. I wish you a good start

Keywords: Java Spring

Added by sheriff on Sat, 05 Feb 2022 13:21:34 +0200