ComponentScanning其他@SpringBootApplications

时间:2015-07-27 16:14:27

标签: java solr spring-boot component-scan

我在防止Spring Boot自动配置某些类时遇到了一些困难(在此示例中为:SolrAutoConfiguration)。为了说明我设置了一个简化的例子:

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

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

问题出现是因为多个子项目正在使用Solr 5.2.1,但Spring Boot仅与4.x兼容。在最终的应用程序中(示例中的module-b),我想在所有模块中导入所有@SpringBootApplication类,同时阻止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);
    }
}

此操作失败,因为@SpringBootApplication通过@ComponentScan没有特定排除的任何实例仍然加载SolrAutoConfiguration

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

我已经尝试在最终excludeFilters上使用@SpringBootApplication,但这还没有找到解决方案。

2 个答案:

答案 0 :(得分:15)

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 Boot在存在多个@EnableAutoConfiguration / @SpringBootApplication注释的情况下加载autoconfig类。

答案 1 :(得分:3)

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

相关问题