简体   繁体   中英

Is it possible to exclude nested configuration from autoconfiguration in Spring Boot application?

Suppose we have a Spring Boot application and autoconfiguration with several configurations defined inside it

@Configuration
@AutoConfigureBefore(MainAutoConfiguration.class)
public class TestAutoConfiguration {

    ....

    @Configuration
    public static class FirstNestedConfiguration {
        ...
    }

    @Configuration
    public static class SecondNestedConfiguration {
        ...
    }
}

this class is providing via external library dependency and all conditions are satisfied, so all beans in these configurations are loading.

Nevertheless, I need to exclude beans provided in FirstNestedConfiguration Is it possible to do it?

UPD: as it's simple Spring Boot application, it runs as

@SpringCloudApplication
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

without any explicit @ComponentScan configuration

You might use excludeFilters in @ComponentScan like this:

@ComponentScan(value = {'your.package'},
    excludeFilters = @Filter(TestAutoConfiguration.class))

Also, if you want to exclude specific autoconfiguration globally, use properties:

spring:
  autoconfigure.exclude: your.package.TestAutoConfiguration 

However, please, note that this way you exclude outer configuration. According to this issue it's not possible to exclude inner configuration.

Does the profile approach works for you? Look for With the @Profile annotation section

 @Profile("ConfigOne")
 @Configuration

Configuration spring documentation

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM