Spring No unique bean type

I have a little problem in Spring with two service components.

I have this component:

@Component public class SmartCardWrapper 

and this one:

 @Component public class DummySmartCardWrapper extends SmartCardWrapper 

Service startup, but Spring fails:

 org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.cinebot.smartcard.SmartCardWrapper] is defined: expected single matching bean but found 2: [dummySmartCardWrapper, smartCardWrapper] 

Why doesn't he use class names?

+8
spring javabeans code-injection
source share
1 answer

This is one of Spring’s most basic concepts - management inversion.

You do not need to declare your dependencies using their implementation types (to avoid communication with the implementation). Instead, you can declare them using interfaces or superclasses and make Spring find the appropriate implementation class in context.

In other words, beans do not differ in their implementation classes, because you can change the bean implementation class without changing the beans that depend on it. If you want to distinguish between different beans of the same type, use logical bean instead:

 @Autowired @Qualifier("smartCardWrapper") private SmartCardWrapper smardCardWrapper; @Autowired @Qualifier("dummySmartCardWrapper") private SmartCardWrapper dummySmardCardWrapper; 
+6
source share

All Articles