spring的元注解
Spring的元注解:注解上的注解。
1.@Target(ElementType.TYPE)
使⽤java.lang.annotation.Target可以定义其使⽤时机,在定义时要时要指定java.lang.annotaton.ElementType的枚举值之⼀。
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)public @interface 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();}
下⾯看⼀下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}
ANNOTATION_TYPE: 注解只能修饰注解,不能修饰其他的东西CONSTRUCTOR: 注解只能修饰构造⽅法FIELD: 注解只能修饰属性(成员变量)LOCAL_VARIABLE: 注解只能修饰局部变量METHOD: 注解只能修饰⽅法PACKAGE: 注解只能修饰包
PARAMETER: 注解只能修饰⽅法的参数TYPE: 注解只能修饰类、接⼝、枚举
2.@Retention(RetentionPolicy.RUNTIME)
@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}
SOURCE:编译程序处理完Annotation信息后就完成任务,编译时忽略CLASS:编译程序将Annotation存储于class档中,JVM忽略
RUNTIME:编译程序将Annotation储存于class档中,可由VM使⽤反射机制的代码所读取和使⽤。
3.Documented
Documented 注解表明这个注解应该被 javadoc⼯具记录. 默认情况下,javadoc是不包括注解的. 但如果声明注解时指定了 @Documented,则它会被 javadoc 之类的⼯具处理, 所以注解类型信息也会被包括在
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)public @interface Documented {}
4.@Inherited
这是⼀个稍微复杂的注解类型. 它指明被注解的类会⾃动继承. 更具体地说,如果定义注解时使⽤了 @Inherited 标记,然后⽤定义的注解来标注另⼀个⽗类, ⽗类⼜有⼀个⼦类(subclass),则⽗类的所有属性将被
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)public @interface Inherited {}