我有两个来自框架的注释。我经常在同一字段上使用这两个注释。因此,我试图创建一个包含两者的“组合”注释。
但我不知道这是否可能:
现有注释(我无法控制):
@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 "";
}
Result:注释是不适用于注释类型.
问题:有机会吗?
不幸的是,您无法执行此操作,因为无法扩展注释。
java中有类似注解继承的东西吗? https://stackoverflow.com/questions/7761513/is-there-something-like-annotation-inheritance-in-java
当我第一次回答这个问题时,我最初对 Spring 框架解决这个缺点的方法感到困惑,他们使用元级别注释(例如 @Component 作为 @Controller/@Configuration 等的元注释)作为一种解决方法。
See: https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-annotation-config https://docs.spring.io/spring/docs/3.2.x/spring-framework-reference/html/beans.html#beans-annotation-config
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)