我正在使用 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/