精彩文章免费看

springboot项目scanBasePackages和exclude 迁移到配置文件

在实际开发中想把@SpringBootApplication 扫描包 scanBasePackages 和 排除功能exclude 放到配置文件 @Configuration中,方便打jar包(不要@SpringBootApplication)……搜索查询了很久,没有找到方法 手动捂脸…… 最后……如下

如下springboot启动类

@SpringBootApplication(scanBasePackages = { "com.demo.workflow", "com.demo.xiaoxiao.swagger"}, ,exclude = {SecurityAutoConfiguration.class}) 迁移到config配置文件

  • 将启动类配置信息(需要删除类的扫码包信息以及需要排除的类SecurityAutoConfiguration )
  • @SpringBootApplication(scanBasePackages = {
            "org.flowable.ui.modeler.repository",
            "org.flowable.ui.modeler.service",
            "org.flowable.ui.modeler.rest.app",
            "org.flowable.ui.modeler.rest.api",
            "org.flowable.ui.common.service",
            "org.flowable.ui.common.repository",
            "org.flowable.ui.common.tenant",
            "com.demo.workflow",
            "com.demo.xiaoxiao.swagger"}
            , exclude = {SecurityAutoConfiguration.class})
    public class Application{
     public static void main(String[] args) {
            SpringApplication.run(Application.class, args);
    
  • 配置文件迁移 扫描的包的注解用@ComponentScan 排除的包用注解@EnableAutoConfiguration(exclude 进行排除
  • @Configuration
    @ComponentScan(basePackages = {
            "org.flowable.ui.modeler.repository",
            "org.flowable.ui.modeler.service",
            "org.flowable.ui.modeler.rest.app",
            "org.flowable.ui.modeler.rest.api",
            "org.flowable.ui.common.service",
            "org.flowable.ui.common.repository",
            "org.flowable.ui.common.tenant",
            "com.demo.workflow",
            "com.demo.skull.swagger"}
    @EnableAutoConfiguration(exclude =  {SecurityAutoConfiguration.class})
    public class MyAppConfiguration {
    

    注意 如下方式迁移是无效的
    @ComponentScan(basePackages = {"com.demo.workflow", "com.demo.skull","org.flowable.ui.modeler", "org.flowable.ui.common"}
    , excludeFilters = @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = SecurityAutoConfiguration.class)

    为什么这么迁移? 其实 很简单,我们看下注解源码就知道了,截取一部分

    scanBasePackages 对应的刚好是 @ComponentScan
    exclude 对应的刚好是 @EnableAutoConfiguration(exclude

    所以将启动类的配置更改到配置中心如上