我可以使用 @AliasFor 来子类化注释吗?

发布于 2025-01-18 09:33:51 字数 1166 浏览 2 评论 0原文

我有一些要更改的注释:添加或更改默认值。注释不能分类,但是有[@aliasfor] [1]的注释。我试图使用它。

我得到了

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface BaseAnnotation {
    String target();

    boolean redirect) default false;

}

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface DerivedAnnotation {
    @AliasFor(annotation = BaseAnnotation.class, attribute = "target")
    String target() default "";
    
    @AliasFor(annotation = BaseAnnotation.class, attribute = "redirect")
    boolean redirect() default true;
}

我想为target参数添加默认值,然后更改redirect参数的默认值。

然后我应用了注释,

@GetMapping("/v1/...")
@DerivedAnnotation
public void myEndpoint(...) {

但是失败了

org.springframework.beans.factory.beandefinitionstoreexception:失败 读取候选组件类:文件[/...controller.class];嵌套 例外是 org.springframework.core.annotation.annotationConfigurationException: @Aliasfor在注释中属性“重定向”声明 [...派生的Annotation]在 注释[... baseannotation]不是meta-present。

它是什么,可以做什么?

I have some annotation which I want to change: add or change default values. Annotation cannot be subclassed, but there is an annotation of [@AliasFor][1]. I tried to use it.

I got

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD, ElementType.TYPE})
public @interface BaseAnnotation {
    String target();

    boolean redirect) default false;

}

and I made

@Retention(RetentionPolicy.RUNTIME)
@Target({ElementType.METHOD})
public @interface DerivedAnnotation {
    @AliasFor(annotation = BaseAnnotation.class, attribute = "target")
    String target() default "";
    
    @AliasFor(annotation = BaseAnnotation.class, attribute = "redirect")
    boolean redirect() default true;
}

i.e. I wanted to add default value for target parameter and change default value for redirect parameter.

Then I have applied my annotation

@GetMapping("/v1/...")
@DerivedAnnotation
public void myEndpoint(...) {

but it is failing with error

org.springframework.beans.factory.BeanDefinitionStoreException: Failed
to read candidate component class: file [/...Controller.class]; nested
exception is
org.springframework.core.annotation.AnnotationConfigurationException:
@AliasFor declaration on attribute 'redirect' in annotation
[...DerivedAnnotation] declares an alias for attribute 'redirect' in
annotation [...BaseAnnotation] which is not meta-present.

What is it and what can be done?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文