Can I use @Autowired
Spring 4.x @Component
s with a @ConditionalOnProperty
to choose implementations of a Feature based on a featuretoggles.properties file?
public class Controller {
@Autowired
private Feature feature;
}
@Component
@ConditionalOnProperty(name = "b", havingValue = "off")
public class A implements Feature {
}
@Component
@ConditionalOnProperty(name = "b", havingValue = "on")
public class B implements Feature {
}
@Configuration
@PropertySource("classpath:featuretoggles.properties")
public class SomeRandomConfig {
}
With a src/main/resources/featuretoggles.properties file:
b = on
(That the name of the toggle "b" and the name of the class "B" match is coincidence; it's not my aim to have these equal, the toggle could have any name.)
This fails to auto-wire feature
in the Controller
with an UnsatisfiedDependencyException
, saying "No qualifying bean of type 'Feature' available: expected at least 1 bean that qualifies as autowire candidate".
I know I can realize this with a @Configuration
class that chooses a @Bean
depending on the property. But when I do that I have to add a new Configuration class each time I add a feature toggle, and those Configuration classes will be highly similar:
@Configuration
@PropertySource("classpath:featuretoggles.properties")
public class FeatureConfig {
@Bean
@ConditionalOnProperty(name = "b", havingValue = "on")
public Feature useB() {
return new B();
}
@Bean
@ConditionalOnProperty(name = "b", havingValue = "off")
public Feature useA() {
return new A();
}
}
I did what you're trying to do by following this guide. First step was to write a
Condition
......then an annotation using that condition.
You can add
featuretoggles.properties
to the environment with the @PropertySource annotation.