We created custom spring annotation @ProfileRegex
to upgrade simple @Profile
functionality
here is a usage example:
@Configuration
public class MySomeConfigClass {
@Bean
@ProfileRegex("^(test|dev)$")
public OurCustomInterface getLocalImplementation() {
return new LocalImplementationOfOurCustomInterface();
}
@Bean
@ProfileRegex("^(qa|prod)$")
public OurCustomInterface getCloudImplementation() {
return new CloudImplementationOfOurCustomInterface();
}
@Bean
public void CustomClassTakesOurInterfaceAsArg getSomething(OurCustomInterface ourCustomInterface) {
return new CustomClassTakesOurInterfaceAsArg(ourCustomInterface);
}
}
in this example Intellij highlight ourCustomInterface
in 3d method as an error with info:
Could not autowire. There is more than one bean of 'OurCustomInterface' type
if I will change that example to simple @Profile
it will understand that there is a difference between those 2 beans and not show error, however simple @Profile
is not enough for us, as our profiles are more complex then just test
, dev
, qa
, prod
and we do require regex to avoid listing all of them like:
@Profile({"profile1", "profile2", "profile3", ***, "profileX"})
So the question is, how to make it treat our @ProfileRegex
in the same way as simple @Profile
and stop highlighting the error?
I did add @Primary
to one of those, and it fixes the error, but our team thinks it is not really good approach, as there are truly no primary or secondary as all really depends on profile condition.
P.S. regardless error in IDEA, code compiles fine and working as needed, I just don't like error highlight in IDEA
any hints? thanks in advance
UPDATE (add an implementation of our @ProfileRegex
):
@despadina created a gist based on this article https://raymondhlee.wordpress.com/2015/05/31/using-spring-4-condition-to-control-bean-registration/.
Just our version supports multiple regex, that's the only difference: https://gist.github.com/dpomaresp/1cbfdd13e2985b5796e30ee1714d8785
So by just adding @ProfileRegex("^(test|dev)$") at the class or method level should work