Can I negate (!) a collection of spring profiles?
config, java, spring, spring-boot
Solution
Since Spring 5.1 (incorporated in Spring Boot 2.1) it is possible to use a profile expression inside profile string annotation (see the description in Profile.of(..) for details).
So to exclude your bean from certain profiles you can use an expression like this:
@Profile("!dev & !prof1 & !prof2")
Other logical operators can be used as well, for example:
@Profile("test | local")
Problem
Is it possible to configure a bean in such a way that it wont be used by a group of profiles? Currently I can do this (I believe): ``` @Profile("!dev, !qa, !local") ``` Is there a neater notation to achieve this? Let's assume I have lots of profiles. Also, if I have a Mock and concrete implementation of some service (or whatever), Can I just annotate one of them, and assume the other will be used in all other cases? In other words, is this, for example, necessary: ``` @Profile("dev, prof1, prof2") public class MockImp implements MyInterface {...} @Profile("!dev, !prof1, !prof2") //assume for argument sake that there are many other profiles public class RealImp implements MyInterface {...} ``` Could I just annotate one of them, and stick a `@Primary` annotation on the other instead? In essence I want this: ``` @Profile("!(dev, prof1, prof2)") ``` Thanks in advance!