如何将多个注释合并为一个? [英] 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.
这篇关于如何将多个注释合并为一个?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!