编程语言
首页 > 编程语言> > Java注解的 含义 作用 分类

Java注解的 含义 作用 分类

作者:互联网

文章目录

一.注解的定义

注解是一种为程序元素设置元数据的方法

注解的作用是把数据存储起来, 在某一个时刻去调用.


注解是继承自Annotation

二. 注解的作用

  1. 作为特定的标记, 告知编译器一些信息. 例如方法的@Override 注解, 用于编译器去检验方法的重写是否符合规范.
  2. 编译时动态处理, 例如动态生成代码, 例如Lombok提供的一些注解@Data, 来动态的生成getter setter toString 等等方法
  3. 运行时动态处理, 作为额外的信息载体,例如@Controller层的请求映射的路径

一个注解最重要的是解析这个注解的代码, 否则这个注解就没有连注释都不如.

三. 注解的分类

  1. 标准注解: Override(方法重写) Deprecated(过时的) SuppressWarings(忽略某些警告)
  2. 元注解: @Target @Retention @Inherited @Documented 这些注解的作用是用于定义注解的注解
  3. 自定义注解

3.1 元注解

元注解是用于修饰注解的注解, 通常用在注解的定义上.
@Target : 注解的作用目标
@Retention : 注解的生命周期
@Inherited : 是否允许子类继承该注解
@Documented: 注解是否应当被包含在JavaDoc上

3.1.1@Target的作用

@Target 用于描述所修饰的注解使用范围.

public enum ElementType {
    /**  用于类 接口  枚举 
Class, interface (including annotation type), or enum declaration */ 
    TYPE,

    /**   作用于属性上 例如Spring中的 Autowired
Field declaration (includes enum constants) */
    FIELD,

    /** Method declaration */
    METHOD,

    /**  用于方法的参数上 例如Spring的RequestParam
    Formal parameter declaration */
    PARAMETER,

    /** Constructor declaration */
    CONSTRUCTOR,

    /** Local variable declaration */
    LOCAL_VARIABLE,

    /** Annotation type declaration */
    ANNOTATION_TYPE,

    /** Package declaration */
    PACKAGE,

    /**
     * Type parameter declaration
     *  用于泛型类的实参上 
     * @since 1.8
     */
    TYPE_PARAMETER,

    /**
      表示该注解可以用于任何地方. 
     * Use of a type
     *
     * @since 1.8
     */
    TYPE_USE,

    /**
     * Module declaration.
     *
     * @since 9
     */
    MODULE
}
3.1.2@Retention的作用

标注注解被保留的时间长短

Retention 的源码

@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Retention {
    /**
     * Returns the retention policy.
     * @return the retention policy
     */
    RetentionPolicy value();
}

RetentionPolicy 的源码, 定义了几种类型

public enum RetentionPolicy {
    /**
      注解能在源文件(java文件)中保留, 编译后在class文件中被去除. 
      例如Override 也是用的@Retention(RetentionPolicy.SOURCE)进行修饰. 
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,

    /**
       注解能在源文件(java文件)中保留, 编译后在class文件中也会保留. 
     * Annotations are to be recorded in the class file by the compiler
     * but need not be retained by the VM at run time.  This is the default
     * behavior.
     */
    CLASS,

    /**
      能够在运行时获取该注解的相关信息.  
      例如Spring的Autowired注解, 通过反射将用到的bean实例注入进来. 
     * Annotations are to be recorded in the class file by the compiler and
     * retained by the VM at run time, so they may be read reflectively.
     *
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}
3.1.3 其他注解

@Documented: 注解是否应当被包含在JavaDoc上 .
@Inherited : 是否允许子类继承该注解. 注解是否具有可继承性. 例如某个注解修饰了一个父类, 并且这个注解是用@Inherited修饰的, 那么该父类的子类就会自动的被该注解修饰.

标签:Java,Target,含义,declaration,RetentionPolicy,3.1,注解,Retention
来源: https://blog.csdn.net/qq_33229669/article/details/113914653