本文介绍了SpringBootApplication 在 ComponentScanning 其他 @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.在最终的应用程序(示例中的模块 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);
    }
}

这失败了,因为通过 @ComponentScan 获取的任何 @SpringBootApplication 实例在没有特定排除的情况下,仍然加载 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,而不是像发行说明中的​​ excludes.

Note that it should say spring.autoconfigure.exclude, not excludes as in the release notes.

这有助于防止 Spring Boot 在存在多个 @EnableAutoConfiguration/@SpringBootApplication 注释时加载自动配置类.

This helps prevent Spring Boot from loading autoconfig classes in the presence of multiple @EnableAutoConfiguration/@SpringBootApplication annotations.

这篇关于SpringBootApplication 在 ComponentScanning 其他 @SpringBootApplications 时排除的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-11 02:11