启动报错:
No qualifying bean of type [class1] available: expected single
matching bean but found 2,class1,classImp1
这个问题的根源在于,class1和classImp1都被加入Bean容器管理,但是@AutoWired进行自动注入,没有指定具体class1的ID,那么spring通过反射机制使用byName自动注入策略,将配置文件的value注入,而不是Id值,这就导致找到两个相同的value值,无法选择注入哪一个,导致报错。
通过查找资料,发现可以在@Autowired下面再加上@Qualifier(“类名”)注释,来指定注入具体的注入类。
但是又报出如下错误
No qualifying bean of type [class1] available: expected at least 1 bean which qualifies as autowire candidate
刚开始感觉莫名其妙,后来发现在applicationContext.xml中对于classImpl1的bean已经新增了classImpl1,并且id设置的并不是classImpl1的类名。
所以设置@Qualifier(“bean中classImpl1的id值”)
最终代码
在applicationContext.xml文件中
< bean id="class123Dao" class="xxx.xxx.ClassDaoImpl">
在classServiceImpl(调用ClassDaoImpl类的Service中)
@Autowired
@Qualifier("class123Dao")
ClassDao classDao; //ClassDao是ClassDaoImpl的接口
问题解决。
更多推荐
Spring项目启动报错No qualifying bean of type [xxx] available: expected single matching
发布评论