SpringBootApplication 在ComponentScanning 其他@SpringBootApplications 时排除

SpringBootApplication exclude when ComponentScanning other @SpringBootApplications

我在阻止 Spring 引导自动配置一些 classes 时遇到了一些困难(在本例中:SolrAutoConfiguration)。为了说明,我设置了一个大大简化的示例:

https://github.com/timtebeek/componentscan-exclusions

实际上有大约 20 多个内部 @SpringBootApplication 项目,每个项目都有自己的依赖关系。 (不理想/不是我的想法,但现在很难离开。)

出现问题是因为多个子项目都在使用Solr 5.2.1,但是Spring Boot只兼容4.x。在最终应用程序(示例中的模块 b)中,我想在我的所有模块中导入所有 @SpringBootApplication classes,同时防止 SolrAutoConfiguration 来自 运行:

@ComponentScan("project") // Broad scan across all company jars
@SpringBootApplication(exclude = { SolrAutoConfiguration.class }) // Failing exclude
public class ModuleBApp {
    public static void main(final String[] args) {
        SpringApplication.run(ModuleBApp.class, args);
    }
}

这会失败,因为通过 @ComponentScan 获取的 @SpringBootApplication 的任何实例在没有特定排除的情况下仍会加载 SolrAutoConfiguration

当组合多个 @SpringBootApplication class 时,如何正确排除自动配置 class?

我已经尝试在我的期末考试 @SpringBootApplication 中使用 excludeFilters,但还没有找到解决方案。

目前我认为这是不可能的。问题 gh-2939 and gh-2435 与此问题完全相关。

Spring Boot 1.3.0.M3 引入了使用属性排除自动配置的功能: https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.3.0-M3-Release-Notes

spring.autoconfigure.exclude=org.springframework.boot.autoconfigure.solr.SolrAutoConfiguration

请注意,它应该是 spring.autoconfigure.exclude,而不是发行说明中的​​ excludes

这有助于防止 Spring 引导在存在多个 @EnableAutoConfiguration/@SpringBootApplication 注释的情况下加载自动配置 类。