0

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

Bryksin
  • 2,322
  • 3
  • 19
  • 31
  • 1
    see if it helps https://stackoverflow.com/questions/43168881/can-i-negate-a-collection-of-spring-profiles or https://stackoverflow.com/questions/35429168/how-to-conditionally-declare-bean-when-multiple-profiles-are-not-active – Ori Marko Jun 30 '20 at 11:35
  • What logic is implemented in your application to process including/excluding beans based on the custom conditional annotation `ProfileRegex`? My guess is that the logic to process your annotation isn't being loaded, resulting in none of the annotated beans being excluded (hence, the beans are always loaded _both_) – Michiel Jun 30 '20 at 19:57
  • @Michiel no, no. Logic is working and in run time the appropriate bean is instantiated, it is just in Intellij syntax analyses goes crazy as it doesn't know how to treat our custom `ProfileRegex` Regarding it implementation, it is pretty much similar to native spring `Profile` just with additional logic to handle regex expression. we will publish implementation later as open-source to GitHub and ill add link to it – Bryksin Jul 01 '20 at 08:28

0 Answers0