spring注解@interface

在写注解的时候,发现注解上面还有4个注解:
1.@Target: 用来描述注解使用范围
2.@Retention:用来描述在什么时期使用
3.@Documented:表示注解是否可以用到api文档中
4.@ Inherited:表示注解是否具备可继承性,可继承意思是,如果父类有该注解,则子类也继承这个注解。通过反射获取子类时,也能获取到该注解。这一点在写aop切面时,还是挺有用的,可以判断是否拥有注解。

@Target 其源码如下

    /**
     * Returns an array of the kinds of elements an annotation type
     * can be applied to.
     * @return an array of the kinds of elements an annotation type
     * can be applied to
     */
    ElementType[] value();
}

里面是一个ElementTypede 枚举类,该枚举类如下:

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

public enum RetentionPolicy {
    /**
     * 注释只在源代码级别保留,编译时被忽略
     */
    SOURCE,
    /**
     * 注释将被编译器在类文件中记录
     * 但在运行时不需要JVM保留。这是默认的
     * 行为.
     */
    CLASS,
    /**
     *注释将被编译器记录在类文件中
     *在运行时保留VM,因此可以反读。
     * @see java.lang.reflect.AnnotatedElement
     */
    RUNTIME
}

你可能感兴趣的:(spring注解@interface)