I have an interface which is defined in two places like that:
@Configuration
public class AppContext {
@Bean
public SomeInterface usageOne() {
return new SameImpl();
}
@Bean
public SomeInterface usageTwo() {
return new SameImpl(someOtherConfig);
}
@Bean
public Client clientOne(SomeInterface usageOne) {
return new Client(usageOne);
}
@Bean
public OtherClient clientTwo(SomeInterface usageTwo) {
return new OtherClient(usageTwo);
}
}
My client implementation classes do not have any annotations only required constructor. How to qualify the correct interface implementation usage in that case? I don't want to use @Primary
as in my case it's semantically incorrect to name one of the usages as primary (they are in some sense equal). I need to pass the same interface with the same implementation class but configured differently for specific use cases of respected clients. I was thinking that naming the parameter by which I inject the implementation to the bean creation method is enough, but Spring complains with: required a single bean, but 2 were found
. I don't understand how should I use the @Qualifier
annotation.
I'm running with Spring Boot 2.0.4.RELEASE and respected beans and clients created in separate configuration classes so I cannot just call usageTwo()
method when creating OtherClient
like that: new OtherClient(usageTwo());
as this method is not available in clients configuration class.