springmvc注入类 NoUniqueBeanDefinitionException: No qualifying bean of type [] is defined: expected single错误

在springmvc中注入服务时用@Service

当有两个实现类时都标明@Service后则会出现异常:

nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type [com.james.dao.impl.BaseDao] is defined: expected single matching bean but found 2: 

这是因为都标明了@Service会自动注入,这时会导致不清楚实际运行时实例化哪一类。

解决:

方法一

实际运用哪一个实现类就在哪个类上注入@Service,另外一个不标注

方法二 

都不标注@Service,在resource文件夹中配置 applicationContext.xml,表明实际运行哪个实现类。

<bean id="serviceImpl" class="com.james.service.impl.ServiceImpl"></bean>

 

posted @ 2017-11-14 20:07  欢醉  阅读(9808)  评论(0编辑  收藏  举报