[java] An introduction to annotations in java, how to customize annotations, what types are there, and what are their functions

java annotation

Annotation definition

Java annotations are used to provide metadata for Java code. As metadata, annotations do not directly affect your code execution, but there are some types of annotations that can actually be used for this purpose. Java annotations were added to Java starting from Java5.

The first thing to be clear is that annotations have no practical effect, annotations only serve as markers, nothing more

Annotation type

1. The standard annotations that come with Java
include @Override, @Deprecated, @SuppressWarnings, etc. After using these annotations, the compiler will check them.

2. Meta-annotations
Meta-annotations are annotations used to define annotations, including @Retention, @Target, @Inherited, @Documented, @Repeatable, etc.
Meta-annotations are also standard annotations that come with Java, but they are used to modify annotations, which are quite special.

3. Custom annotations
Users can define annotations according to their needs.

How to customize annotations?
In fact, it is very simple, the example is as follows

package com.spring.annotation;

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

/**
 * 请求映射
 *
 * @author ez4sterben
 * @date 2023/07/22
 */
@Target({
    
    ElementType.TYPE,ElementType.METHOD})
@Retention(RetentionPolicy.RUNTIME)
public @interface RequestMapping {
    
    

    String value() default "";
}

How to use it can refer to the blogger’s blog
[imitation of spring] 1. Read the class with @RequestMapping and @Controller annotations through reflection and simulate the request path to call the method

Composition of annotations

Annotate the main class

package java.lang.annotation;
public interface Annotation {
    
    

    boolean equals(Object obj);

    int hashCode();

    String toString();

    Class<? extends Annotation> annotationType();
}

ElementType, an enumeration class, is placed in @Target to indicate where the annotation can take effect

package java.lang.annotation;

public enum ElementType {
    
    
    TYPE,               /* 类、接口(包括注释类型)或枚举声明  */

    FIELD,              /* 字段声明(包括枚举常量)  */

    METHOD,             /* 方法声明  */

    PARAMETER,          /* 参数声明  */

    CONSTRUCTOR,        /* 构造方法声明  */

    LOCAL_VARIABLE,     /* 局部变量声明  */

    ANNOTATION_TYPE,    /* 注释类型声明  */

    PACKAGE             /* 包声明  */
}

Used to declare the lifecycle of annotations

package java.lang.annotation;
public enum RetentionPolicy {
    
    
    SOURCE,            /* Annotation信息仅存在于编译器处理期间,编译器处理完之后就没有该Annotation信息了  */

    CLASS,             /* 编译器将Annotation存储于类对应的.class文件中。默认行为  */

    RUNTIME            /* 编译器将Annotation存储于class文件中,并且可由JVM读入 */
}

Annotated schema

The role of annotations

1. Generate documents, and generate javadoc documents through the metadata identified in the code.

2. Compilation check, let the compiler check and verify during compilation through the metadata identified in the code.

3. Dynamic processing at compile time, dynamically processed through the metadata identified in the code at compile time, such as dynamically generating code.

4. Dynamic processing at runtime, dynamically processing through the metadata identified in the code at runtime, such as using reflection to inject instances

Guess you like

Origin blog.csdn.net/qq_51383106/article/details/131872877