您的位置:首页 > 其它

No qualifying bean of type [] is defined

2017-08-30 17:31 639 查看
No qualifying bean of type [] is defined expected single matching bean but found 2.

使用@autowired时,默认按bean类型装配,当有多个同类型bean时,继续使用名称装配,当名称没有符合的时候,抛出异常。

该异常说明某个装配位置,找到了2个同类型的bean,并且没有与装配位置变量名称一样的bean。

解决方法: 检查@autowired处定义的变量名与需要装载示例名称是否一致。

例如:

public interface IHomePageService {}

@Service
@Transactional
public class HomePageServiceImpl implements IHomePageService{}

@Controller
public class HomePageController {
@Autowired
private IHomePageService homePageService;
}


HomePageController中使用@autowired注入了一个IHomePageService,

Spring从bean中查找IHomePageService类型的bean装载,

结果发现了两个bean,都是IHomePageService类型.

一个是@service层的实现类HomePageServiceImpl的bean,名字是homePageServiceImpl.

一个是接口IHomePageService的bean,名字是IHomePageService.

因为两个bean类型一直,类型匹配无法确定唯一,@autowired会继续使用名称规则匹配,会使用装配处的变量名homePageService匹配找到的两个bean 的名称,发现还是无法匹配,所以抛出异常。

这个时候把@autowired出的变量名改为homePageServiceImpl就可以匹配到唯一一个bean了,装载成功。

@Controller
public class HomePageController {
@Autowired
private IHomePageService homePageServiceImpl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐