@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
public @interface Component {
String value() default "";
}
决定一个注解的靶,即这个注解可以注解到哪些地方。
@Target注解自己的@Target(ElementType.ANNOTATION_TYPE),表示注解在注解上
通过ElementType枚举选择
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, //在class文件时保留, 在加载到VM时会被丢弃,即只在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 //会被加载进到VM, VM中通过反射是可以获取到此注解
}
参考 :
https://blog.csdn.net/qq_43390895/article/details/100175330
原文:https://www.cnblogs.com/wftop1/p/14770005.html