SSM异常:No qualifying bean of type `CompanyDao` available expected at least 1 bean which qualifies

1、边写代码边做测试,由于采用聚合架构及Maven的方式创建项目,一个父项目(Project)会有多个子模块(Module),每个子模块都会在resources目录下创建spring文件夹,用于存放spring相应的配置文件,于是测试时遇到了以下的异常:
java.lang.IllegalStateException: Failed to load ApplicationContext
	at org.springframework.test.context.cache.DefaultCacheAwareContextLoaderDelegate.loadContext(DefaultCacheAwareContextLoaderDelegate.java:125)
	at org.springframework.test.context.support.DefaultTestContext.getApplicationContext(DefaultTestContext.java:107)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.injectDependencies(DependencyInjectionTestExecutionListener.java:117)
	at org.springframework.test.context.support.DependencyInjectionTestExecutionListener.prepareTestInstance(DependencyInjectionTestExecutionListener.java:83)
	at org.springframework.test.context.TestContextManager.prepareTestInstance(TestContextManager.java:242)
Caused by: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'companyServiceimpl': Unsatisfied dependency expressed through field 'companyDao'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'CompanyDao' 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:586)
	at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:91)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:372)
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'CompanyDao' 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:1504)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1101)
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1062)
	at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:583)
	... 42 more
2、主要的异常信息为:

(1) java.lang.IllegalStateException: Failed to load ApplicationContext. (没有加载到ApplicationContext相应的配置文件)
(2) UnsatisfiedDependencyException: Error creating bean with name ‘companyServiceimpl’: Unsatisfied dependency expressed through field ‘companyDao’;(错误创建bean,名称为:companyServiceImpl,无法依赖bean:companyDao)
(3) No qualifying bean of type ‘CompanyDao’ available: expected at least 1 bean which qualifies as autowire candidate.(没有找到符合类型的bean:CompanyDao)

3、由于SSM框架整合时,Spring需要依赖MyBatis对数据库进行操作,而ApplicationContext-service.xml文件已经加载到了,但是没有加载到ApplicationContext-dao.xml文件,于是会出现异常:No qualifying bean of type ‘CompanyDao’(没有找到符合类型的bean:CompanyDao)(测试时,下图中左边栏也出现类似的异常,却不知道异常出现在哪里,其实还是ApplicationContext-dao.xml文件没有被加载到)

SSM异常:No qualifying bean of type `CompanyDao` available expected at least 1 bean which qualifies_第1张图片

4、将上图中的@ContextConfiguration注解扫描的范围扩大,于是就能扫描并加载到ApplicationContext-dao.xml文件
classpath  加载当前项目类路径下的配置文件
classpath* 加载所有项目(包含子模块)类路径下的配置文件
@ContextConfiguration("classpath*:spring/spring-*.xml")
5、于是成功加载到ApplicationContext-dao.xml配置文件后,运行代码,上述的异常已不存在

SSM异常:No qualifying bean of type `CompanyDao` available expected at least 1 bean which qualifies_第2张图片

你可能感兴趣的:(代码异常,Java,Spring,SpringMvc,MyBatis,Execption)