简体   繁体   中英

How do I tell Spring to skip loading a Configuration class based on a property?

I know that with the @Profile annotation, you can tell Spring to only load a certain class when using the specified profile, like this:

@Configuration
@Profile("dev")
public class MyCustomConfigurationClass {
    // this will only be instantiated when the "dev" profile is active
}

However I'm wondering if there's some equivalent way of doing that for a given application property / environment variable? Here's some pseudo code to illustrate what I want to do:

@Configuration
@OnlyInstantiateWhen(property = "${my.property}", value = "true")
public class MyCustomConfigurationClass {
    // this would theoretically only be instantiated when
    // the value of my.property is true, either in application.properties
    // or in an environment variable
}

Is anything like this possible?

Take a look at implementation of your own condition or using @ConditionalOnProperty() like answers for [this question] suggest. ( Conditional spring bean creation )

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