如何将多个注释合并为一个?

mem*_*und 9 java spring annotations

我有两个来自框架的注释。我经常在同一个字段上使用这两个注释。因此,我试图创建一个包含两者的“组合”注释。

但我不知道这是否可能:

现有的注释(我无法控制):

@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 "";
}
Run Code Online (Sandbox Code Playgroud)

正在尝试创建的自定义注释

@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
@ApiParam
@ApiModelProperty
public @interface SwaggerParam {
     String name() default "";
}
Run Code Online (Sandbox Code Playgroud)

结果:注解不适用于注解类型

问:有机会吗?

Plo*_*log 5

不幸的是,您无法执行此操作,因为无法扩展注释。

java中有类似注解继承的东西吗?

当我第一次回答这个问题时,我最初对 Spring 框架解决这个缺点的方法感到困惑,他们使用元级别注释(例如 @Component 作为 @Controller/@Configuration 等的元注释)作为一种解决方法。

请参阅:https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-annotation-config