Spring注解之@Retention

时间:2025-04-14 16:04:49

作用是定义被它所注解的注解保留多久,一共有三种策略,定义在RetentionPolicy枚举中:

package java.lang.annotation;
/**
* Annotation retention policy. The constants of this enumerated type
* describe the various policies for retaining annotations. They are used
* in conjunction with the {@link Retention} meta-annotation type to specify
* how long annotations are to be retained.
*
* @author Joshua Bloch
* @since 1.5
*/
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文件;但是不被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
}