gpt4 book ai didi

java - 使用自定义注释的组件扫描

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:09:40 25 4
gpt4 key购买 nike

我正在使用 maven 依赖项在另一个 spring boot 应用程序中将 spring boot 项目作为 jar 使用。仅当我从微服务启用自定义注释时,我才想对 jar 进行组件扫描。

@SpringBootApplication
//@ComponentScan({"com.jwt.security.*"}) To be removed by custom annotation
@MyCustomAnnotation //If I provide this annotation then the security configuration of the jar should be enabled.
public class MicroserviceApplication1 {

public static void main(String[] args) throws Exception {

SpringApplication.run(MicroserviceApplication1.class, args);

}

}

请提出一些想法。

最佳答案

在您的图书馆中:

@Configuration
@ComponentScan(basePackages = { "com.jwt.security" })
public class MyCustomLibConfig{

}


@Retention(RUNTIME)
@Target(TYPE)
@Import(MyCustomLibConfig.class)
public @interface MyCustomAnnotation{

@AliasFor(annotation = Import.class, attribute = "value")
Class<?>[] value() default { MyCustomLibConfig.class };
}

因此,在您的应用程序中您可以使用注释

@SpringBootApplication
@MyCustomAnnotation //If I provide this annotation then the security configuration
of the jar should be enabled.
public class MicroserviceApplication1 {

public static void main(String[] args) throws Exception {
SpringApplication.run(MicroserviceApplication1.class, args);
}

}

关于java - 使用自定义注释的组件扫描,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47565859/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com