spring容器中能拥有两个同种类型的bean吗。我有两个dao类同时实现一个接口,这两个接口注入时报了异常。

时间:2021-10-05 20:35:19
No unique bean of type [com.ncl.pes.dao.iface.business.EpmPositionDao] is defined。如果只注入其中一个没错,但如何能把两个同时注入呢???

11 个解决方案

#1


在实现类上指定bean的名称
@Service(value = "xxx")

@Resource(name="xxx")

#2


楼上的方法不错

#3


这样还是报这个错,有人告诉我不能实现接口要用抽象类,因为用接口bean会向接口转型.所以就判断有两个同种类型的bean。一开始我也认为时@Autowired的问题,因为我这是按类型收缩的。但我试了@Resource也不行。

#4


在实现类加上@Component("test1"),@Component("test2"),要注入的时候加上@Resource(name="test1")或者@Resource(name="test2"),测试了一下是行的··

#5


我的是完全相同的两个类,就改了个类名。然后同时注入。
@Repository
public class EpmPositionSqlMapDao extends BaseSqlMapDao implements EpmPositionDao{另一个是
@Component("test1")
public class TestDao extends BaseSqlMapDao implements EpmPositionDao{ 
service类是:@Service
public class TestService {

@Resource(name="test1")
private EpmPositionDao epmPositionDao;


这是报的异常:Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.ncl.pes.dao.iface.business.EpmPositionDao] is defined: expected single matching bean but found 2: [epmPositionSqlMapDao, test1]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:621)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
... 60 more

#6


引用 5 楼 zhangxianchao2012 的回复:
我的是完全相同的两个类,就改了个类名。然后同时注入。
@Repository
public class EpmPositionSqlMapDao extends BaseSqlMapDao implements EpmPositionDao{另一个是
@Component("test1")
public class TestDao extends BaseSqlM……


我测试过了,两个类实现同一个接口,在一个实现类加上@Component("test1"),一个加上@Component("test2")
在引用的时候加上@Resource(name="test1")或者@Resource(name="test2"),就会指定用哪个实现类,不加resource注解才会出现上面那个错误

#7


哦,我在试一下。还是谢谢帮忙啊。

#8


应该是其他的service用到实现类时没用@Resource,要把所有使用到实现类的注解由@Resource改为 @Autowired,应该就行了。我就改了一个。

#9


由 @Autowired改为@Resource。搞错了

#10


我就在类前加abstract,运行后就没报错了。

#11


加abstract不行,对不起大家啦,千万别加。

#1


在实现类上指定bean的名称
@Service(value = "xxx")

@Resource(name="xxx")

#2


楼上的方法不错

#3


这样还是报这个错,有人告诉我不能实现接口要用抽象类,因为用接口bean会向接口转型.所以就判断有两个同种类型的bean。一开始我也认为时@Autowired的问题,因为我这是按类型收缩的。但我试了@Resource也不行。

#4


在实现类加上@Component("test1"),@Component("test2"),要注入的时候加上@Resource(name="test1")或者@Resource(name="test2"),测试了一下是行的··

#5


我的是完全相同的两个类,就改了个类名。然后同时注入。
@Repository
public class EpmPositionSqlMapDao extends BaseSqlMapDao implements EpmPositionDao{另一个是
@Component("test1")
public class TestDao extends BaseSqlMapDao implements EpmPositionDao{ 
service类是:@Service
public class TestService {

@Resource(name="test1")
private EpmPositionDao epmPositionDao;


这是报的异常:Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No unique bean of type [com.ncl.pes.dao.iface.business.EpmPositionDao] is defined: expected single matching bean but found 2: [epmPositionSqlMapDao, test1]
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:621)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:412)
... 60 more

#6


引用 5 楼 zhangxianchao2012 的回复:
我的是完全相同的两个类,就改了个类名。然后同时注入。
@Repository
public class EpmPositionSqlMapDao extends BaseSqlMapDao implements EpmPositionDao{另一个是
@Component("test1")
public class TestDao extends BaseSqlM……


我测试过了,两个类实现同一个接口,在一个实现类加上@Component("test1"),一个加上@Component("test2")
在引用的时候加上@Resource(name="test1")或者@Resource(name="test2"),就会指定用哪个实现类,不加resource注解才会出现上面那个错误

#7


哦,我在试一下。还是谢谢帮忙啊。

#8


应该是其他的service用到实现类时没用@Resource,要把所有使用到实现类的注解由@Resource改为 @Autowired,应该就行了。我就改了一个。

#9


由 @Autowired改为@Resource。搞错了

#10


我就在类前加abstract,运行后就没报错了。

#11


加abstract不行,对不起大家啦,千万别加。