繁体   English   中英

如何在注释中使用自定义注释参数的值

[英]How to use the value of custom annotation parameter inside the annotation

想要创建包含多个注释的注释,但希望使其更具可配置性,以便在所有场景中使用更可行

@SpringBootApplication
@ServletComponentScan(basepackages="com.example.commons.traceability")
@ComponentScan(basePackages={
    "com.example.commons.security",
    "com.example.commons.restTemplate",
    "com.example.commons.logging",
})
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Microservice{
    String[] scanBasePackages() default {};
}

我想在 Spring 应用程序文件中使用这个应用程序,但也想让 componentScan 更具可配置性,以便除了默认包之外,其他包也可以包含在组件扫描中

@Microservice(scanBasePackages={"com.example.myproject"})
public class MySpringApplciation{
    public static void main (final String[] args){
    // some code
    }
}

所以在上面的代码中,如果我传递了 scanBasePackages 的值,那么这些包也应该包含在组件扫描中。

有没有办法在自定义注释中包含 componentScan 内的 scanBasePackages 的值?

(自定义)注释上的注释称为元注释。

简而言之:你很幸运。 虽然 Java 不允许开箱即用,但 Spring 对这类事情有一个特殊的注释:@AliasFor。

我有一个类似的问题,我在这里找到了答案: Passing annotation properties to meta-annotations

(我自己不太幸运,因为我需要这种东西来减少我的 JUnit5 样板注释。)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM