Spring Bean自动装配失败

tech2023-05-29  55

运行项目,启动报错如下:

at com.meituan.baobab.wtaccess.Application.main(Application.java:51) [classes/:?] Caused by: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'baseDataManagerImpl': Injection of resource dependencies failed; nested exception is org.springframework.beans.factory.BeanNotOfRequiredTypeException: Bean named 'damServiceAdapter' is expected to be of type 'com.meituan.baobab.wtaccess.adapter.dam.api.DamServiceAdapter' but was actually of type 'com.meituan.baobab.commons.filter.adapter.DefaultDamServiceAdapter'

报错提示bean装配成了另外一个。

在import中查看,导入的是com.meituan.baobab.wtaccess.adapter.dam.api.DamServiceAdapter。应该没有问题。

试着删掉它,可以看到有两个包都存在DamServiceAdapter类 而在代码里是使用@Resource注解,而Resource是通过名字装配的,

所以当有两个类名相同会有冲突。导致装配错误。

解决办法: 修改类名或者使用@Autowire

拓展: @Autowired 与@Resource的区别

最新回复(0)