Spring Profile annotation allows you to select profiles. However if you read documentation it only allows you to select more than one profile with OR operation. If you specify @Profile("A", "B") then your bean will be up if either profile A or profile B is active.
Our use case is different we want to support TEST and PROD versions of multiple configurations. Therefore sometimes we want to autowire the bean only if both profiles TEST and CONFIG1 are active.
Is there any way to do it with Spring? What would be the simplest way?
Since Spring does not provide the AND feature out of the box. I would suggest the following strategy:
Currently
@Profile
annotation has a conditional annotation@Conditional(ProfileCondition.class)
. InProfileCondition.class
it iterates through the profiles and checks if the profile is active. Similarly you could create your own conditional implementation and restrict registering the bean. e.g.In your class:
NOTE: I have not checked for all the corner cases (like null, length checks etc). But, this direction could help.