关于org.springframework.beans.factory.UnsatisfiedDependencyException报错解决问题方法

最近按照网上的demo做了一个springboot的小项目,为了整合springboot和mybatis+xml进行演示,但是在配置时一直报错,纠结了两个小时:

如下是启动springboot是的报错:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'cityDaoImpl': Unsatisfied dependency expressed through field 'sqlSessionTemplate'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionTemplate' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Unsatisfied dependency expressed through method 'sqlSessionTemplate' parameter 0; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'sqlSessionFactory' defined in class path resource [org/mybatis/spring/boot/autoconfigure/MybatisAutoConfiguration.class]: Bean instantiation via factory method failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [org.apache.ibatis.session.SqlSessionFactory]: Factory method 'sqlSessionFactory' threw exception; nested exception is org.springframework.core.NestedIOException: Failed to parse mapping resource: 'class path resource [mybatis-config.xml]'; nested exception is org.apache.ibatis.builder.BuilderException: Error parsing Mapper XML. Cause: java.lang.NullPointerException

调试心得:

没经验的时候是从前往后一直查找,百度网上相关例子,总结起来,查找如下:

在网上看到了许多人都经历过类似的错误,归纳起来有:

  1. Application启动类放置位置问题
  2. 注解配置不完整,如Service实现类没有加@Service,Spring boot无法扫描等
  3. 配置文件等内容有误
  4. Jar包冲突

一个一个查找,第一个问题,因为引用别人的项目,所以不可避免的有些包名和结构发生变化,

1、检查application.properties文件中,关于mybatis.mapper-locations=classpath:mybatis-config.xml的问题,

当时一直以为这个classpath是在加载时去mybatis-config.xml中进行CityMapper的加载,所以在配置mybatis-config.xml时,

一直找不到CityMapper.xml这个文件,导致Error parsing Mapper XML. Cause: java.lang.NullPointerException的问题,所以解决办法,直接在mybatis.mapper-locations=classpath:mybatis-config.xml更改为mybatis.mapper-locations=classpath:mybatis/mapper/*.xml,跳过mybatis-config.xml,直接使用mybatis/mapper/*.xml进行xml的加载

2、在调试过程中,同样发现,Service实现类没有加@Service,导致Spring boot无法扫描,

两个小bug浪费了两个小时,对于springboot小白的我来说太心痛了

  • 2
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
根据提供的引用内容,nacos启动报错ERROR Startup errors : org.springframework.beans.factory.UnsatisfiedDependencyException,这个错误通常是由于依赖注入问题引起的。具体来说,可能是某个bean的依赖项无法满足,或者依赖项没有正确注入。解决此问题的方法如下: 1.检查nacos配置文件是否正确,特别是数据库连接信息是否正确。 2.检查nacos依赖项是否正确,特别是版本是否匹配。 3.检查nacos启动日志,查看哪个bean无法满足依赖项,然后检查该bean的依赖项是否正确注入。 4.如果无法确定问题所在,可以尝试使用调试器进行调试,以查找问题。 下面是一个可能的解决方案: ```java org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘xxxController’: Unsatisfied dependency expressed through field ‘xxxService’; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name ‘xxxServiceImpl’: Unsatisfied dependency expressed through field ‘xxxRepository’; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name ‘xxxRepository’: Invocation of init method failed; nested exception is java.lang.IllegalArgumentException: Not a managed type: class com.xxx.xxx.xxx ``` 根据上述错误信息,我们可以看到,xxxRepository这个bean的初始化方法失败了,原因是它不是一个受管理的类型。这通常是由于没有在应用程序上下文中注册该类型引起的。解决此问题的方法是在应用程序上下文中注册该类型,例如: ```java @Configuration @EnableJpaRepositories(basePackages = "com.xxx.xxx.xxx") @EntityScan(basePackages = "com.xxx.xxx.xxx") public class AppConfig { // ... } ```

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值