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!
Short answer is : You can't.
But there are neat workaround that exists thanks to the
@Conditional
annotation.Create Condition matchers:
Use it
However, this aproach requires Springboot.
From what I understand, what you want to do is be capable of replacing some of your beans with some stub/mock beans for specific profiles. There are 2 ways to address this:
The first option is feasible but difficult. This is because the default behaviour of Spring when providing multiple profiles in
@Profile
annotation is anOR
condition (not anAND
as you would need in your case). This behaviour of Spring is the more intuitive, because ideally each profile should correspond to each configuration of your application (production, unit testing, integration testing etc.), so only one profile should be active at each time. This is the reason OR makes more sense thanAND
between profiles. As a result of this, you can work around this limitation, probably by nesting profiles, but you would make your configuration very complex and less maintainable.Thus, I suggest you go with the second approach. Have a single profile for each configuration of your application. All the beans that are the same for every configuration can reside in a class that will have no
@Profile
specified. As a result, these beans will be instantiated by all the profiles. For the remaining beans that should be distinct for each different configuration, you should create a separate@Configuration
class (for each Spring profile), having all of them with the@Profile
set to the corresponding profile. This way, it will be really easy to tract what is injected in every case.This should be like below:
Last,
@Primary
annotation is used to override an existing beans. When there are 2 beans with the same type, if there is no@Primary
annotation, you will get an instantiation error from Spring. If you define a@Primary
annotation for one of the beans, there will be no error and this bean will be injected everywhere this type is required (the other one will be ignored). As you see, this is only useful if you have a single Profile. Otherwise, this will also become complicated as the first choice.TL;DR: Yes you can. For each type, define one bean for each profile and add a
@Profile
annotation with only this profile.You can simply mark
RealImp
class with@Profile("Production")
(or your own profile name) so that you don't need to specify all other profiles.Yes, you can use
@Primary
as well forRealImp
class, but ensure that you are using the same concept consistently across the whole project i.e., in other words, if you mark some of the bean real implementation classes (meant for actual production environment) as@Profile("Production")
and some of them with@Primary
then the project will become in a messy state.