在我的项目中,我使用预定义的注释@With:
@With(Secure.class)
public class Test { //....
Run Code Online (Sandbox Code Playgroud)
源代码@With:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface With {
Class<?>[] value() default {};
}
Run Code Online (Sandbox Code Playgroud)
我想编写自定义注释@Secure,其效果与之相同@With(Secure.class).怎么做?
如果我这样喜欢怎么办?它会起作用吗?
@With(Secure.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Secure {
}
Run Code Online (Sandbox Code Playgroud)
pio*_*rek 18
从Java语言规范,第9.6章注释类型:
不允许使用扩展条款.(注释类型隐式扩展
annotation.Annotation.)
因此,您无法扩展Annotation.您需要使用其他一些机制或创建识别和处理您自己的注释的代码.Spring允许您在自己的自定义注释中对其他Spring的注释进行分组.但仍然没有扩展.
小智 18
正如piotrek指出的那样,你不能在继承意义上扩展注释.您仍然可以创建聚合其他人的注释:
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface SuperAnnotation {
String value();
}
@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.TYPE})
public @interface SubAnnotation {
SuperAnnotation superAnnotation();
String subValue();
}
Run Code Online (Sandbox Code Playgroud)
用法:
@SubAnnotation(subValue = "...", superAnnotation = @SuperAnnotation(value = "superValue"))
class someClass { ... }
Run Code Online (Sandbox Code Playgroud)
@With(Secure.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Secure {
}
Run Code Online (Sandbox Code Playgroud)
这会奏效.
为了扩展穆罕默德·阿卜杜勒拉赫曼的答案-
@With(Secure.class)
@Target({ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface Secure {
}
Run Code Online (Sandbox Code Playgroud)
这并不会在默认情况下工作,但你可以结合Spring的使用AnnotationUtils。
有关示例,请参见此SO答案。