Spring Boot 应用中出现“org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'sqlSessionFactory'”异常,通常是由于找不到 mybatis-config.xml 文件导致的。该异常的完整信息可能包含以下内容:

org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'driverController': Unsatisfied dependency expressed through field 'driverService'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'driverServiceImpl': Unsatisfied dependency expressed through field 'traceTransportMapper'; nested exception is org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'traceTransportMapper' defined in file [D:\2009A-1\advance-two-group\ruoyi-modules\ruoyi-traces\target\classes\com\ruoyi\traces\mapper\TraceTransportMapper.class]: Unsatisfied dependency expressed through bean property 'sqlSessionFactory'; 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 java.io.FileNotFoundException: class path resource [mybatis/mybatis-config.xml] cannot be opened because it does not exist
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.resolveFieldValue(AutowiredAnnotationBeanPostProcessor.java:660)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:119)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessProperties(AutowiredAnnotationBeanPostProcessor.java:399)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1431)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:619)
	at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:542)
	at org.springframework.beans.factory.support.AbstractBeanFactory.lambda$doGetBean$0(AbstractBeanFactory.java:335)
	at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:234)
	at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:333)
	at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:208)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:955)
	at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:918)
	at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:583)
	at org.springframework.boot.web.servlet.context.ServletWebServerApplicationContext.refresh(ServletWebServerApplicationContext.java:147)
	at org.springframework.boot.SpringApplication.refresh(SpringApplication.java:731)
	at org.springframework.boot.SpringApplication.refreshContext(SpringApplication.java:408)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:307)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1303)
	at org.springframework.boot.SpringApplication.run(SpringApplication.java:1292)
	at com.ruoyi.traces.RuoYiApplication.main(RuoYiApplication.java:18)

解决方法:

  1. 检查配置文件路径: 确保 mybatis-config.xml 文件位于正确的路径下。通常情况下,该文件应该放在 resources 目录下,或者在 application.propertiesapplication.yml 中配置 mybatis.config-location 属性来指定文件路径。
  2. 检查文件存在性: 确保 mybatis-config.xml 文件确实存在,并且没有被意外删除或改名。
  3. 重新构建项目: 尝试清理项目缓存并重新构建项目。这可以解决一些由于项目构建问题导致的配置文件无法找到的问题。
  4. 清理缓存: 清理 IDE 或构建工具的缓存,确保项目能够正确加载配置文件。

如果上述方法都无法解决问题,请检查项目依赖关系,确保 Mybatis 相关的依赖项已正确配置。

Spring Boot 异常: UnsatisfiedDependencyException - 找不到 mybatis-config.xml 文件

原文地址: https://www.cveoy.top/t/topic/oiGq 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录