我有两个来自框架的注释。我经常在同一个字段中使用这两个注释。因此,我正在尝试创建一个包含这两者的“组合”注释。
但我完全不知道这是否可能:
现有注释(我无法控制):
@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 "";
}
结果:注释不适用于注释类型。
问题:有机会吗?
最佳答案
很遗憾,您不能这样做,因为无法扩展注释。
Is there something like Annotation Inheritance in java?
当我第一次回答这个问题时,我最初对 Spring 框架解决这个缺点的方法感到困惑,他们使用元级注释(例如 @Component 作为 @Controller/@Configuration 等的元注释)作为一种变通方法。
关于java - 如何将多个注释合并为一个注释?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47175378/