Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of CDI's @Default qualifier in Spring?

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?

like image 301
baraber Avatar asked Oct 28 '13 18:10

baraber


1 Answers

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"/>
like image 109
baraber Avatar answered Sep 25 '22 05:09

baraber