7. Annotations and custom annotations

1 Annotation
Annotation is very powerful, it can enhance our java code, and at the same time use reflection technology to expand and realize many functions. They are widely used in the bottom layer of the three major frameworks.
Traditionally, we declare through xml text files (as shown in the figure below, but XML is cumbersome and difficult to check), but now the most mainstream development is based on annotations, with a small amount of code, and the framework can automatically generate a lot of codes based on annotations, thereby reducing code amount, the program is more readable. For example, the most popular SpringBoot is completely based on annotation technology.

insert image description here

The annotation design is very delicate, and when I first learned it, I felt that it was alternative or even redundant, or even rubbish. Why do you need @ annotations when you have java code? But after you are proficient, you will be amazed that it can surpass the functions of java code and make java code instantly powerful. Let's experience it slowly.

2 Classification of annotations

Annotations are divided into 3 categories, let's get to know them first:

JDK comes with annotations
meta-annotations
custom annotations

2.1 JDK annotations

There are only 5 annotations of JDK annotations:

@Override :用来标识重写方法
The @Deprecated mark indicates that this method is outdated, but I will use it, don't remind me that it is expired
@SuppressWarnings("deprecation") Ignore the warning
@SafeVarargs jdk1.7 appears, heap pollution, not commonly used
@FunctionallInterface jdk1.8 appears, cooperate Functional programming lambda expressions, not commonly used

2.2 Meta annotations

There are only 5 annotations used to describe annotations:

@Target 注解用在哪里:类上、方法上、属性上等等
@Retention 注解的生命周期:源文件中、字节码文件中、运行中
@Inherited allows sub-annotations to inherit
@Documented Annotations will be included when javadoc is generated. @Repeatable annotations are not commonly used
as repeatable type annotations, which can be used multiple times in the same place. Not commonly used

2.2.1 @Target ElementType…

Describe where annotations exist:

ElementType.TYPE applies to class elements
ElementType.METHOD applies to method level
ElementType.FIELD applies to fields or properties (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 method parameters

2.2.2 @Retention RetentionPolicy…

This annotation defines the length of time that custom annotations are retained. For example, some annotations only appear in the source code and are discarded by the compiler; while others are compiled in the class file; annotations compiled in the class file may be Ignored by the virtual machine, while others will be read when the class is loaded.

insert image description here

Why do you want to divide whether there is or is not in the bytecode file? If there is no time, the reflective technology will not be available, so it will not be able to identify and process. It has 3 values:

SOURCE is valid in the source file (that is, the source file is preserved)
CLASS is valid in the class file (that is, the class is preserved)
RUNTIME is valid at runtime (that is, the runtime is preserved)

3 Custom annotations

Note: The syntax of annotations is different from that of regular 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;

/*本类用于完成自定义注解*/
public class TestAnnotation {
    
    
}
//2.通过@Target注解标记自定义注解的使用位置
/*3.通过元注解@Target规定自定义注解可以使用的位置
* 我们使用"ElementType.静态常量"的方式来指定自定义注解具体可以加在什么位置
* 而且,值可以写多个,格式:@Target({ElementType.XXX,ElementType.XXX}*/
@Target({
    
    ElementType.METHOD,ElementType.TYPE})//可以加在方法&类上
//3.通过@Retention注解标记自定义注解的生命周期
/*4.通过元注解@Retention规则自定义注解的生命周期
* 我们使用"RetentionPolicy.静态常量"的方式来指定自定义注解的生命周期
* 注意:值只能写一个:SOURCE CLASS RUNTIME 3选1 */
@Retention(RetentionPolicy.RUNTIME)//到运行时都有效
//1.定义自定义注解
/*1.首先注意:注解定义的语法与Java不同
* 2.定义自定义注解的格式:@interface 注解名*/
@interface Rice{
    
    
    //5.我们可以给注解进行功能增强--添加注解的属性
    /*5.注意:int age();不是方法的定义,而是给自定义注解添加了一个age属性*/
    //int age();//给自定义注解添加一个普通属性age,类型是int
    int age() default 0;//给自定义注解的普通属性赋予默认值0
    /*6.注解中还可以添加特殊属性value
    * 特殊属性的定义方式与普通属性一样,主要是使用方式不同
    * 注意:特殊属性的名字必须叫value,但是类型不做限制
    * 特殊属性也可以赋予默认值,格式与普通属性一样,不能简写
    * */
    //String value();//定义一个特殊属性value,类型是String
    String value() default "Lemon";//定义特殊属性并给特殊属性赋予默认值
}

//4.定义一个类用来测试自定义注解
//@Rice
class TestAnno{
    
    
    /*测试1:分别给TestAnno类 name属性 eat方法都添加Rice注解
    * 结论:属性上的注解报错了,说明自定义注解可以加在什么位置,由@Target决定*/
    //@Rice//报错了
    String name;
    /*测试2:当我们给Rice注解添加了一个age属性以后,@Rice注解使用时直接报错
    * 结论:当注解没有定义属性时,可以直接使用
    *      当注解定义了属性以后,必须给属性赋值,格式:@Rice(age = 10)*/
    /*测试3:给age属性赋予默认值以后,可以直接使用@Rice注解
    * 不需要给age属性赋值,因为age属性已经有默认值0了*/
    /*测试4:给Rice注解添加了特殊属性value以后,必须给属性赋值
    * 只不过特殊属性赋值时可以简写成 @Rice("Apple")
    * 测试5:如果特殊属性也赋予了默认值,那么可以直接使用这个注解
    * 如果要给注解的所有属性赋值,每条赋值都不能简写*/
    @Rice(age=10,value="orange")
    //@Rice("Apple")
    //@Rice(age = 10)
    //@Rice(10)//报错,不可以简写,普通属性没有这种格式
    public void eat(){
    
    
        System.out.println("干饭不积极,思想有问题");
    }
}

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

Guess you like

Origin blog.csdn.net/weixin_58276266/article/details/131478305