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

标签 java spring maven spring-boot annotations

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

相关文章:

java - 如何在HubProxy signalr android上使用invoke

java - EclipseME - 断点问题

Spring 安全 : How to get the initial target url

java - maven 命令 : "-fn" v. s "-Dmaven.test.failure.ignore=true"中的两个可选参数,有什么区别吗?

java - 在 JBoss 应用程序服务器中,如何确保部署中的所有模块对 jar 库使用相同的类加载器?

java - lwjgl glDrawArrays 给出无效操作错误

java - Spring MVC/JSP。如何创建带有国家/地区列表的多语言组合?

spring - spring框架引用文档3.2.2中@Inject是否有错误?

java - Vaadin8 迁移后 Vaadin Widgetset 编译错误

java - findViewById 中根据名称动态访问