元注解:
最常用的元注解是@Target。使用@Target可以定义Annotation能够被应用于源码的哪些位置:
@Target
Annotation
ElementType.TYPE
ElementType.FIELD
ElementType.METHOD
ElementType.CONSTRUCTOR
ElementType.PARAMETER
@Retention定义Annotation的生命周期:
@Retention
RetentionPolicy.SOURCE
RetentionPolicy.CLASS
RetentionPolicy.RUNTIME
使用@Repeatable这个元注解可以定义Annotation是否可重复 //不常用
@Repeatable
使用@Inherited定义子类是否可继承父类定义的Annotation
@Inherited
必须设置@Target和@Retention,@Retention一般设置为RUNTIME,因为我们自定义的注解通常要求在运行期读取。一般情况下,不必写@Inherited和@Repeatable。
RUNTIME
Sign in to make a reply
厚天独到
元注解:
@Target
最常用的元注解是
@Target
。使用@Target
可以定义Annotation
能够被应用于源码的哪些位置:ElementType.TYPE
;ElementType.FIELD
;ElementType.METHOD
;ElementType.CONSTRUCTOR
;ElementType.PARAMETER
。@Retention
@Retention
定义Annotation
的生命周期:RetentionPolicy.SOURCE
;RetentionPolicy.CLASS
;RetentionPolicy.RUNTIME
。@Repeatable
使用
@Repeatable
这个元注解可以定义Annotation
是否可重复 //不常用@Inherited
使用
@Inherited
定义子类是否可继承父类定义的Annotation
总结:
必须设置
@Target
和@Retention
,@Retention
一般设置为RUNTIME
,因为我们自定义的注解通常要求在运行期读取。一般情况下,不必写@Inherited
和@Repeatable
。