Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring No unique bean of type

i have a little trouble in Spring with two component of a service.

I have this component:

@Component
public class SmartCardWrapper 

and this one:

@Component
public class DummySmartCardWrapper extends SmartCardWrapper

The service autowire both but spring fails due this expection:

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 it doesn't use class names?

like image 233
Tobia Avatar asked Jul 04 '12 08:07

Tobia


People also ask

How do I fix no qualifying bean of type?

The error is “expected at least 1 bean which qualifies as autowire candidate.” The solution is to add the injection class in the ApplicationContext using annotation @Component. The exception “NoSuchBeanDefinitionException: No qualifying bean of type” is resolved if the annotation @Component is added in the Lion class.

What happens if two beans have the same name in Spring?

Spring beans are identified by their names within an ApplicationContext. Therefore, bean overriding is a default behavior that happens when we define a bean within an ApplicationContext that has the same name as another bean. It works by simply replacing the former bean in case of a name conflict.

How is the Spring bean uniquely identify?

This attribute specifies the bean identifier uniquely. In XMLbased configuration metadata, you use the id and/or name attributes to specify the bean identifier(s). This attribute specifies the scope of the objects created from a particular bean definition and it will be discussed in bean scopes chapter.

Can we create two beans of same class in Spring?

If you define two beans of same class, without different bean id or qualifiers ( identifier) , Spring container will not be able to understand which bean to be loaded , if you try to access the bean by passing the classname and you will get NoUniqueBeanDefinitionException as there are two qualifying TestBean.


1 Answers

That's one of the most basic concepts of Spring - Inversion of Control.

You don't need to declare your dependencies using their implementation types (to avoid coupling with implementation). You can declare them using interfaces or superclasses instead, and make Spring find the proper implementation class in the context.

In other words, bean are not distinguished by their implementation classes, because you may want to change implementation class of a bean without changing the beans that depend on it. If you want to distinguish between different beans of the same type, use logical bean names instead:

@Autowired @Qualifier("smartCardWrapper")
private SmartCardWrapper smardCardWrapper;

@Autowired @Qualifier("dummySmartCardWrapper")
private SmartCardWrapper dummySmardCardWrapper;
like image 56
axtavt Avatar answered Nov 09 '22 10:11

axtavt