2018-01-06 15:09:14,361 WARN [org.springframework.web.context.support.XmlWebApplicationContext] - Exception encountered during context initialization - cancelling refresh attempt: org.springframework.beans.factory.UnsatisfiedDependencyException: Error creating bean with name 'blogController': Unsatisfied dependency expressed through field 'categoryService'; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ssm.blog.service.CategoryService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
Caused by: org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type 'com.ssm.blog.service.CategoryService' available: expected at least 1 bean which qualifies as autowire candidate. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}
上面这段异常说创建blogController的时候失败了,UnsatisfiedDependencyException是依赖不满足抛出的异常,因为我在Controller中注入了categoryService。接着NoSuchBeanDefinitionException表示没有这样一个bean,也就是说我的categoryService没有生成bean。
遇到这种情况,要检查一下以下配置:
1) service接口实现类上有没有加@Service注解,注解是不是引用的spring的类?不要导错包
2) 接口有没有写实现类,实现类是实现的对应接口么?比如CategoryServiceImpl implementsCategoryDAO 一不小心根据自动提示,本来应该实现CategoryService,结果实现了CategoryDAO
3) 有没有扫描Service所在的包
<!-- 启动自动扫描 -->
<context:component-scan base-package="com.ssm.blog.*">
<!-- 制定扫包规则 ,不扫描@Controller注解的JAVA类 -->
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
注意这段配置要加在spring父容器中而不是springMVC子容器。因为父容器无法访问自容器的对象。