Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

springboot How to exclude configuration class in dependency dependency

I am using a library dependency which contains a @configuration class that I need to be ignored.

When I do

@SpringBootApplication(exclude={NeedsToBeExcluded.class})
public class Startup {
  public static void main(String[] args) {
    SpringApplication.run(Startup.class, args);
}

I get the Exception

nested exception is java.lang.IllegalStateException: The following classes could not be excluded because they are not auto-configuration classes: NeedsToBeExcluded.class

like image 807
Eduardo Dennis Avatar asked Nov 19 '18 16:11

Eduardo Dennis


People also ask

How do I exclude a configuration class in spring?

If the class is not on the classpath, you can use the excludeName attribute of the annotation and specify the fully qualified name instead. Finally, you can also control the list of auto-configuration classes to exclude by using the spring. autoconfigure. exclude property.

How do I disable auto-configuration in spring boot?

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 is @SpringBootApplication exclude?

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.

What is the difference between @configuration and EnableAutoConfiguration?

How They Differ. The main difference between these annotations is that @ComponentScan scans for Spring components while @EnableAutoConfiguration is used for auto-configuring beans present in the classpath in Spring Boot applications.


1 Answers

you can use @ComponentScan to exclude classes:

@ComponentScan(basePackages = {"package1","package2"},
  excludeFilters = {@ComponentScan.Filter(
    type = FilterType.ASSIGNABLE_TYPE,
    value = {ExcludedConfigClass.class})
  })
like image 184
stacker Avatar answered Oct 19 '22 07:10

stacker