元注解

元注解

修饰注解的注解,对注解进行说明

@Target

1、作用

描述注解的作用位置(即:被修饰的注解可以用在什么地方)

2、修饰的注解详细使用位置

注解可以用于修饰 packages、types(类、接口、枚举、注解类)、类成员(方法、构造方法、成员变量、枚举值)、方法参数和本地变量(如循环变量、catch参数),在定义注解类时使用了@Target 能够更加清晰的知道它能够被用来修饰哪些对象,它的取值范围定义在ElementType 枚举中

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

    /** Field declaration (includes enum constants) */
    FIELD,   //成员变量

    /** Method declaration */
    METHOD,   //方法

    /** 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     //使用类型的任何地方
}

@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 {
    /**
     * Annotations are to be discarded by the compiler.
     */
    SOURCE,   // 源码中保留

    /**
     * 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,  //编译时期保留  默认值

    /**
     * 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  //运行时保留,可通过反射获取注解信息
}

@Documented

作用

描述在使用javadoc工具生成帮助文档时是否其注解信息

@Inherited

作用

使被它修饰的注解具有继承性(例:A被带有@Inherited修饰的注解@SpringBootApplication修饰,其子类B自动具有@SpringBootApplication注解)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值