如何将多个注释合并为一个注释? [英] How to combine multiple annotations to single one?
问题描述
我在一个框架中有两个注释.我经常在同一字段上使用这两个注释.因此,我试图创建一个包含两者的组合"注释.
I have two annotations from a framework. Often I use those two annotations both on the same field. Thus I'm trying to create a "combined" annotation that contains that both two.
但我完全不知道是否可能:
But I don't know if it is possible at all:
现有注释(我无法控制)
@Target({ElementType.PARAMETER, ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiParam {
String name() default "";
}
@Target({ElementType.METHOD, ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ApiModelProperty {
String name() default "";
}
我要创建的
我的自定义批注:
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
@ApiParam
@ApiModelProperty
public @interface SwaggerParam {
String name() default "";
}
结果:注释不适用于注释类型.
问题:有机会吗?
推荐答案
很遗憾,您无法执行此操作,因为无法扩展注释.
Unfortunately you can't do this since it is not possible to extend annotations.
当我第一次回答这个问题时,我最初对这个缺点感到困惑,因为Spring框架方法使用元级别注释(例如@Component作为@ Controller/@ Configuration等的元注释)作为一种解决方法.
When I first answered this I was initially confused by the Spring framework approach to this shortcoming whereby they use meta level annotations (such as @Component as a meta annotation for @Controller/@Configuration etc.) as a sort of workaround.
请参阅: 查看全文