簡體   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