Unsatisfied dependency expressed through field dao,千万不要画蛇添足

各位小伙伴千万别画蛇添足。使用Springboot的时候。当你想用两个数据库查询两张表提高数据库的性能,你可千万得看好了

Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: 
Error creating bean with name 'teacherGradeController': Unsatisfied dependency expressed through field 'service'; 
nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'teacherGradeServiceImpl': Unsatisfied dependency expressed through field 'dao'; nested exception is org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.qf.dao.TeacherGradeDao' available: expected single matching bean but found 2: teacherGradeDaoImpl,teacherGradeDao

Description:

Field dao in com.qf.service.Impl.TeacherGradeServiceImpl required a single bean, but 2 were found:
	- teacherGradeDaoImpl: defined in file [E:\Protect\springboot\springbootInterceptor\target\classes\com\qf\dao\Impl\TeacherGradeDaoImpl.class]
	- teacherGradeDao: defined in file [E:\Protect\springboot\springbootInterceptor\target\classes\com\qf\dao\TeacherGradeDao.class]


Action:

Consider marking one of the beans as @Primary, updating the consumer to accept multiple beans, or using @Qualifier to identify the bean that should be consumed

这个错误,其实就在你的启动类,一方面是看看放在哪里,你如果放在子包里则他不会扫描的,比如放在(bean,service。。。)这种的父包,他才会扫描这个。
还有一种就是你是不是在启动类中写入 @MapperScan(dao层) 这个玩意,千万别写,这个注解的意思就是扫描dao层包的接口,因为你在这个查询两个数据库的时候,他是有dao层的实现类的,你在扫描dao接口的注解你肯定找不到,所以方法就是 删除再启动类中写的 @MapperScan 就OK了

@Primary 这个是标明哪个是主库
如果是双数据库的话 最好是在每个dao层上面写上 @Mapper

  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,"Unsatisfied dependency expressed through field"错误通常发生在Spring Boot应用程序中,表示在注入依赖时出现问题。这个错误通常是由于以下原因之一引起的: 1. 依赖注入失败:可能是由于没有正确配置或声明依赖项所导致的。请确保在类中正确注解了需要注入的依赖项,并且依赖项的配置正确。 2. 循环依赖:可能是由于循环依赖关系导致的。循环依赖是指两个或多个类之间相互依赖,形成了一个闭环。在Spring中,循环依赖是不允许的,因此会导致"Unsatisfied dependency expressed through field"错误。解决循环依赖问题的方法是重新设计类之间的依赖关系,或者使用构造函数注入代替字段注入。 3. 依赖项未找到:可能是由于依赖项未正确配置或未在应用程序的类路径中找到所导致的。请确保依赖项的配置正确,并且相关的依赖项已经添加到项目的依赖项列表中。 以下是解决"Unsatisfied dependency expressed through field"错误的一些常见方法: 1. 检查依赖项的注解和配置,确保它们正确地注入到需要它们的类中。 2. 检查是否存在循环依赖关系,并尝试重新设计类之间的依赖关系。 3. 检查依赖项的配置和类路径,确保它们正确地配置和添加到项目中。 4. 如果使用的是Spring Boot和MyBatis-Plus,可以尝试重新启动应用程序并启用调试模式,以查看更详细的错误信息和条件报告。 希望这些信息对您有所帮助!如果您有任何其他问题,请随时提问。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值