如何将多个注释组合到单个注释中?

时间:2017-11-08 09:00:02

标签: 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 "";
}
我试图创建的

我的自定义注释

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

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

问题:有机会吗?

1 个答案:

答案 0 :(得分:1)

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

Is there something like Annotation Inheritance in java?

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

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