In CDI, I could do this:
// Qualifier annotation
@Qualifier
@inteface Specific{}
interface A {}
class DefaultImpl implements A {}
@Specific
class SpecificImpl implements A {}
And then in a class:
@Inject
A default;
@Inject
@Specific
A specific;
It works because of the @Default
qualifier automatically assigned to injection points not specifying any qualifiers.
But I'm working with Spring and was unable to perform that.
Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException
The problem is that the "default" injections (without qualifiers) are already used in lot of code I can't change, and I need to provide another possible implementation of A
for my users.
I'm aware that I could inject my new implementation by bean name, but I would like to avoid it.
Is there anything in Spring that could help me achieve it?
Someone pointed at me that @Primary does exactly this. I tried and it works perfectly :
@Primary
class DefaultImpl implements A {}
In my case DefaultImpl was in xml :
<bean id="defaultImpl" class="DefaultImpl" primary="true"/>
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With