I have a situation where I want to enable clients to inject their own bean implementing a common interface into my class but otherwise use a default bean.
As an example:
public class TestClass {
// Clients can inject this, otherwise use a default...
@Autowired
private ConfigInjector configInjector;
}
My defined interface and default implementation:
public interface ConfigInjector {
String getConfig();
}
@Component
public class DefaultConfigInjector implements ConfigInjector {
public String getConfig() {
return "DEFAULT CONFIG"
}
}
An external client wants to inject this implementation of my ConfigInjector interface:
@Component
public class ExternalConfigInjector implements ConfigInjector {
public String getConfig() {
return "EXTERNAL CONFIG"
}
}
Using @Autowired in TestClass, if the client attempts to use TestClass with his own ConfigInjector implementation (ExternalConfigInjector), then more than one ConfigInjector implementation will exist, resulting in a NoUniqueBeanDefinitionException exception.
So, how can I share my TestClass with clients allowing them to use their own ConfigInjector to override my default?