Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Disable Spring Configuration if one of several profiles is present

As expected, this Configuration will not load if moduleTestis 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?

like image 881
neu242 Avatar asked Sep 13 '16 11:09

neu242


People also ask

How do I keep my spring boot profiles different?

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.

How do I bypass spring configuration?

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.

How do you define beans for a specific profile?

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.

Can you use @component together with profile?

Yes, @Profile annotation can be used together with @Component on top of the class representing spring bean.


1 Answers

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.

like image 84
Michael Böckling Avatar answered Sep 29 '22 23:09

Michael Böckling