'How can I exclude a specific @Configuration class from my Spring Boot application?

Spring Boot 2.3.12 (I can't update to a newer version for reasons out of my control).

I have defined my main application class with specific scan base packages like this:

@SpringBootApplication(scanBasePackageClasses = {
                            MyApplication.class,
                            org.otherpackage.ComponentScanMarker.class
                            }
                        )
@ComponentScan(
            excludeFilters = {
                    @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value = HateoasConfiguration.class)
                    }
                )
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(MyApplication.class);
        application.run(args);
    }
}

What I'm trying to accomplish is both:

A) include a package outside the application's base package (hence the org.otherpackage.ComponentScanMarker.class reference in the @SpringBootApplication annotation)

and

B) exclude the HateoasConfiguration class completely*.

I've also tried this:

@SpringBootApplication
@ComponentScan(
            basePackageClasses = {
                            MyApplication.class,
                            org.otherpackage.ComponentScanMarker.class
            },
            excludeFilters = {
                    @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value = HateoasConfiguration.class)
            }
                )

That results in HateoasConfiguration being loaded despite the excludeFilters.

Another option I tried:

@SpringBootApplication(scanBasePackageClasses = {
                            MyApplication.class,
                            org.otherpackage.ComponentScanMarker.class
                        },
                        exclude = HateoasConfiguration.class
                        )

That results in an exception at startup with the message:

The following classes could not be excluded because they are not auto-configuration classes:
    - org.springframework.hateoas.config.HateoasConfiguration

I can't get it to work, no matter what combination of annotation properties I try. Either HateoasConfiguration gets loaded despite the attempt to exclude it, or @Components in org.otherpackage don't get loaded. I've looked at a few different similar questions and answers, but none of them include the need for both goals.

How can I accomplish both needs, to include multiple base packages for component scanning, and exclude a specific @Configuration class that's on the classpath?


* This question really has nothing to do with Spring HATEOAS, it's just an example of a @Configuration class that is on the classpath but I want Spring Boot to ignore. Here are the annotations present on that class (source code here):

@Configuration(proxyBeanMethods = false)
@EnablePluginRegistries({ LinkDiscoverer.class })
public class HateoasConfiguration {


Solution 1:[1]

Have you tried this ?

@SpringBootApplication(scanBasePackageClasses = {
                            MyApplication.class,
                            org.otherpackage.ComponentScanMarker.class
                            },
exclude = { HateoasConfiguration.class }
                        )
@ComponentScan(
            excludeFilters = {
                    @ComponentScan.Filter(type=FilterType.ASSIGNABLE_TYPE, value = HateoasConfiguration.class)
                    }
                )
public class MyApplication {

    public static void main(String[] args) {
        SpringApplication application = new SpringApplication(MyApplication.class);
        application.run(args);
    }
}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Gaurav