1、在测试Spring整合Mybatis,进行Junit测试时,遇到了以下报错
java.lang.IllegalStateException: Failed to load ApplicationContext
at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:132)
at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:123)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'accountServiceImpl': Unsatisfied dependency expressed through field 'accountDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'it.com.dao.AccountDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:643)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:130)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'it.com.dao.AccountDao' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
at org.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoMatchingBeanFound(DefaultListableBeanFactory.java:1717)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1273)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1227)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:640)
... 42 more
2、主要报错内容为
(1)java.lang.IllegalStateException: Failed to load ApplicationContext --> 没有加载到ApplicationContext相应的配置文件
(2)UnsatisfiedDependencyException: Error creating bean with name 'accountServiceImpl': Unsatisfied dependency expressed through field 'accountDao'; --> 创建名为“accountServiceImpl”的bean时出错,无法依赖bean:accountDao
(3)NoSuchBeanDefinitionException: No qualifying bean of type 'it.com.dao.AccountDao' available: expected at least 1 bean which qualifies as autowire candidate. --> 没有找到符合类型的bean:AccountDao
3、因为做得是Spring整合Mybatis,由异常报错 NoSuchBeanDefinitionException: No qualifying bean of type 'it.com.dao.AccountDao' available 可知没有加载到dao包下的文件,遂到Mybatis配置类 MybatisConfig 中查看,发现是Mapper映射文件扫描包路径写错
将路径修改为” it.com.dao “ 后,运行代码,运行正常,结果正常显示