You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Spring Boot 3中@ComponentScan excludeFilters失效问题求助

Spring Boot 3迁移后@ComponentScan excludeFilters失效的解决方案

针对Spring Boot 2迁移到3后,主类中@ComponentScan的excludeFilters无法过滤指定配置类的问题,可尝试以下几种解决方案:

  • 合并@ComponentScan配置到@SpringBootApplication
    Spring Boot 3中,@SpringBootApplication本身集成了@ComponentScan,手动额外添加@ComponentScan可能导致扫描逻辑冲突。建议将扫描配置直接整合到@SpringBootApplication注解中:

    @EnableJms
    @EnableCaching
    @EnableConfigurationProperties
    @SpringBootApplication(
            scanBasePackages = "com.xxxxx",
            excludeFilters = {
                    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = MAppConfig.class),
                    @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, value = MBaseConfig.class)
            }
    )
    @Push(value = PushMode.MANUAL, transport = Transport.WEBSOCKET)
    @Theme(value = "manager-app")
    public class ManagerSpringBootApplication implements AppShellConfigurator {
        public static void main(String[] args) {
            SpringApplication.run(ManagerSpringBootApplication.class, args);
        }
    }
    

    同时注意修正main方法的static修饰符,原代码中遗漏了该修饰符。

  • 更换过滤器类型为正则匹配
    若ASSIGNABLE_TYPE类型的过滤器在Spring Boot 3中存在逻辑变化,可改用正则匹配类的全限定名:

    @SpringBootApplication(
            scanBasePackages = "com.xxxxx",
            excludeFilters = {
                    @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com\\.xxxxx\\.xxx\\.manager\\.MAppConfig"),
                    @ComponentScan.Filter(type = FilterType.REGEX, pattern = "com\\.xxxxx\\.xxx\\.manager\\.MBaseConfig")
            }
    )
    
  • 排查配置类的其他加载路径
    检查目标配置类是否通过@Import注解、自动配置类,或者META-INF/spring.factories文件被间接加载。开启Spring Boot的debug日志(在application.properties中设置logging.level.org.springframework=DEBUG),搜索目标类名,查看其加载来源,针对性处理。

  • 在配置类上添加条件注解
    若以上方法均无效,可直接在MAppConfig和MBaseConfig上添加条件注解,使其在Spring Boot 3环境下不生效:

    @Configuration
    // 基于Spring Boot 2特有的类判断,仅在存在该类时生效
    @ConditionalOnClass(name = "org.springframework.boot.web.servlet.error.ErrorMvcAutoConfiguration")
    public class MAppConfig {
        // 配置内容
    }
    

内容的提问来源于stack exchange,提问作者karthik_raghavendra

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.02 22:46:11