How about the following approach:
interface Something {} public class FirstBean implements Something {} public class SecondBean implements Something{}
Now the configuration will look like this:
@Configuration public class MyConfiguration { @Bean(name = "hello") @ConditionalOnProperty(name = "some.property", havingValue = true) public Something helloBean() { return new FirstBean(); } @Bean(name = "hello") @ConditionalOnProperty(name = "some.property", havingValue = false) public Something secondBean() { return new SecondBean(); } @Bean @DependsOn("hello") public MyDependantBean dependantBean() { return new MyDependantBean(); } }
The idea is to create a "Something" bean anyway (even if it's an empty implementation), so the dependent bean will depend on something anyway.
I have not tried this myself, you know, spring is full of magic, but probably worth a try :)
source share