As expected, this Configuration will not load if moduleTest
is an active profile:
@Profile({"!moduleTest"})
@Configuration
public class UserConfig {
}
However, this configuration will load:
@Profile({"!moduleTest", "!featureTest"})
@Configuration
public class UserConfig {
}
Is there a way to have a Configuration that will not load if moduleTest
or featureTest
is active?
The solution would be to create more property files and add the "profile" name as the suffix and configure Spring Boot to pick the appropriate properties based on the profile. Then, we need to create three application. properties : application-dev.
To make a configuration in Spring Boot, you need to create a class and annotate it with @Configuration . Usually, in the configuration class, you can define a beans object. But if you want to override built-in configuration, you need to create a new class that extends the built-in class.
Use @Profile on a Bean Let's start simple and look at how we can make a bean belong to a particular profile. We use the @Profile annotation — we are mapping the bean to that particular profile; the annotation simply takes the names of one (or multiple) profiles.
Yes, @Profile annotation can be used together with @Component on top of the class representing spring bean.
There is a simple (undocumented, but I think officially supported) way of doing this:
@Profile("!moduleTest & !featureTest")
By default Spring is using logical OR, this forces it to logical AND.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With