28 API 7 annotations and custom annotations

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 at the bottom of the three frameworks.
Traditionally, we use XML text file declaration (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 spring boot is implemented entirely based on annotation technology.

The annotation design is very exquisite. When I first learned it, I felt it was very alternative, even redundant, or even garbage. Why do you need @ annotations with 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 contamination, not commonly used
@The functional interface JDK1.8 appears, which is not commonly used in conjunction with the lambda expression of functional programming

2.2 yuan notes

There are only 5 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 applies to the method level
ElementType.FIELD applies to a field or property (member variable)
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 the parameter applied to 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 RUNTIME (i.e. RUNTIME retention)

3 custom annotation

Note: the syntax of annotations is different from that of conventional java

package cn.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 annotations*/
public class TestAnotation {
//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 "ElemetType. Static constant" to specify where custom annotations can be added
    *   Moreover, multiple values can be written in the format @ Target({ElementType.xxx,ElementType.xxx})*/
//3. Mark the life cycle of user-defined annotation through @ Retention annotation
    /*4.Customize the annotation lifecycle through the meta annotation @ Retention rule
    *   We use "Retention. Static constant" to specify the life cycle of custom annotations
    *   Note: only one value can be written: SOURCE CLASS RUNTIME*/
    @Retention(RetentionPolicy.RUNTIME)//Valid until runtime
//1. Define custom 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 annotation -- add the attribute of the 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 attribute of the custom annotation
    /*6.In the annotation, you can also add a special attribute value
    * 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 and assign special attributes

//4. Define a class to test custom annotations
class TesAnno{
    /*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*/
    String name;
    /*Test 2: after we added an age attribute to the Rice annotation, the @ Rice annotation directly reported an error when used
    * Conclusion: when the annotation does not define attributes, it can be used directly
    *      After the annotation defines the attribute, the attribute must be assigned a value in the 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 the 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") / / special attributes can be abbreviated
    //@Rice(age = 10) / / normal attribute assignment form
    //@Rice(10) / / 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");

Keywords: Java

Added by mlai4167 on Wed, 29 Sep 2021 03:34:50 +0300