【异常】No qualifying bean of type [xxx] is defined: expected single matching bean but found 2 [xxx,xxx]

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/yhzhaohy/article/details/89641376

Spring项目启动的时候,报以下异常:

No qualifying bean of type [xxx] is defined: expected single matching bean but found 2 [xxx1,xxx2]

通过调查,发现[xxx1,xxx2]中的xxx2是同事在applicationContext-xxx.xml文件中新增的一个Bean元素,而xxx1是在项目中原有的,这两个Bean元素对应的是同一个类。而在项目中引入该元素的写法,并没有进行相应的修改,还是下面的写法:

@Autowired
private XXX xxx;

解决上述异常的方法很简单,只要给Spring指明此时需要的是哪个Bean元素即可。

@Autowired
@Qualifier("xxx2")
private XXX xxx;

以后,只要遇到 【expected single matching bean but found】这类的问题,使用@Qualifier进行区分即可。

猜你喜欢

转载自blog.csdn.net/yhzhaohy/article/details/89641376
今日推荐