当ComponentScanning其他@SpringBootApplications时,SpringBootApplication排除 [英] SpringBootApplication exclude when ComponentScanning other @SpringBootApplications
问题描述
我在防止Spring Boot自动配置某些类(在此示例中为SolrAutoConfiguration)时遇到了一些困难。为了说明我设置了一个简化的例子:
I'm having some difficulty preventing Spring Boot from auto configuring some classes (in this example: SolrAutoConfiguration). To illustrate I've setup a much reduced example:
https://github.com/timtebeek/componentscan-exclusions
实际上有20多个内部 @SpringBootApplication
项目,每个项目都有自己的依赖项。 (不理想/不是我的想法,但现在很难离开。)
In reality there's some 20+ internal @SpringBootApplication
projects, each with their own dependencies coming together. (Not ideal / not my idea, but hard to move away from now.)
问题出现是因为多个子项目正在使用Solr 5.2.1,但Spring Boot只是兼容4.x.在最终的应用程序中(示例中的module-b)我想在所有模块中导入所有 @SpringBootApplication
类,同时阻止 SolrAutoConfiguration
来自运行:
The problem arises because multiple subprojects are using Solr 5.2.1, but Spring Boot is only compatible with 4.x. In the final application (module-b in the example) I want to import all @SpringBootApplication
classes across all my modules, while preventing SolrAutoConfiguration
from running:
@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);
}
}
这失败了,因为任何<$ c $的实例c> @SpringBootApplication 通过 @ComponentScan
获取而没有特定排除,仍然加载 SolrAutoConfiguration
。
This fails, because any instance of @SpringBootApplication
picked up through the @ComponentScan
without the specific exclude, still loads SolrAutoConfiguration
.
在组合多个 @SpringBootApplication
类时,如何正确排除自动配置类?
What can I do to properly exclude a auto configuration class when combining multiple @SpringBootApplication
classes?
我已经尝试在我的最终 @SpringBootApplication
excludeFilters >,但尚未找到解决方案。
I've already tried to work with excludeFilters
on my final @SpringBootApplication
, but that hasn't yet lead to a solution.
推荐答案
Spring Boot 1.3.0.M3引入了排除自动配置的功能使用属性:
https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-1.3.0-M3-Release-Notes
Spring Boot 1.3.0.M3 introduced functionality to exclude autoconfiguration using properties: 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
,而不是排除
。
Note that it should say spring.autoconfigure.exclude
, not excludes
as in the release notes.
这有助于防止Spring Boot fr om在存在多个 @EnableAutoConfiguration
/ @SpringBootApplication
注释的情况下加载autoconfig类。
This helps prevent Spring Boot from loading autoconfig classes in the presence of multiple @EnableAutoConfiguration
/@SpringBootApplication
annotations.
这篇关于当ComponentScanning其他@SpringBootApplications时,SpringBootApplication排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!