Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SpringBootApplication exclude when ComponentScanning other @SpringBootApplications

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

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.)

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);
    }
}

This fails, because any instance of @SpringBootApplication picked up through the @ComponentScan without the specific exclude, still loads SolrAutoConfiguration.

What can I do to properly exclude a auto configuration class when combining multiple @SpringBootApplication classes?

I've already tried to work with excludeFilters on my final @SpringBootApplication, but that hasn't yet lead to a solution.

like image 530
Tim Avatar asked Jul 27 '15 16:07

Tim


People also ask

What is exclude in @SpringBootApplication?

exclude – To exclude specific auto-configuration classes. scanBasePackages – A list of base packages to scan for spring components and beans.

How do I exclude a class from autoconfiguration?

If you find that specific auto-configure classes are being applied that you don't want, you can use the exclude attribute of @EnableAutoConfiguration to disable them. If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead.

What does @SpringBootApplication annotation does internally?

Spring Boot @SpringBootApplication annotation is used to mark a configuration class that declares one or more @Bean methods and also triggers auto-configuration and component scanning. It's same as declaring a class with @Configuration, @EnableAutoConfiguration and @ComponentScan annotations.

Can we have more than one @SpringBootApplication?

In this example we are having two SpringBoot applications that we want to merge into one and for that we will create a new SpringBoot application for package them both and add the two old applications as maven dependencies. Then we will ask Spring to boot-up the application.


2 Answers

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

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

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

like image 64
Tim Avatar answered Sep 29 '22 01:09

Tim


Currently I think it's not possible. The issues gh-2939 and gh-2435 relate to exactly this problem.

like image 23
joshiste Avatar answered Sep 29 '22 03:09

joshiste