java SpringBootApplication 在 ComponentScanning 其他 @SpringBootApplications 时排除
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31658170/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
SpringBootApplication exclude when ComponentScanning other @SpringBootApplications
提问by Tim
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:
我在阻止 Spring Boot 自动配置某些类时遇到了一些困难(在本例中:SolrAutoConfiguration)。为了说明,我设置了一个大大简化的示例:
https://github.com/timtebeek/componentscan-exclusions
https://github.com/timtebeek/componentscan-exclusions
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.)
实际上有大约 20 多个内部@SpringBootApplication
项目,每个项目都有自己的依赖项。(不理想/不是我的想法,但很难从现在开始。)
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:
出现问题是因为多个子项目都在使用Solr 5.2.1,而Spring Boot只兼容4.x。在最终的应用程序(示例中的模块 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);
}
}
This fails, because any instance of @SpringBootApplication
picked up through the @ComponentScan
without the specific exclude, still loads SolrAutoConfiguration
.
这失败了,因为@SpringBootApplication
通过@ComponentScan
没有特定排除的任何实例仍然加载SolrAutoConfiguration
.
What can I do to properly exclude a auto configuration class when combining multiple @SpringBootApplication
classes?
组合多个@SpringBootApplication
类时,如何正确排除自动配置类?
I've already tried to work with excludeFilters
on my final @SpringBootApplication
, but that hasn't yet lead to a solution.
我已经尝试excludeFilters
在我的 final 上使用它@SpringBootApplication
,但这还没有找到解决方案。
回答by Tim
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 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
Note that it should say spring.autoconfigure.exclude
, not excludes
as in the release notes.
请注意,它应该说spring.autoconfigure.exclude
,而不是excludes
像发行说明中那样。
This helps prevent Spring Boot from loading autoconfig classes in the presence of multiple @EnableAutoConfiguration
/@SpringBootApplication
annotations.
这有助于防止 Spring Boot 在存在多个@EnableAutoConfiguration
/@SpringBootApplication
注释的情况下加载自动配置类。