报错如下:
NoSuchBeanDefinitionException:No qualifying bean of type found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency.
错误原因:bean注入失败
解决方法:
1.在配置文件中启动注解功能<mvc:annotation-driven/>
2.在配置文件中添加扫描目录<context:component-scan>
3.应该在实现类上面标记@Service等注解,而不是在接口上标记。
spring-mvc.xml如下所示:
<!-- 自动扫描该包,使SpringMVC认为包下用了@controller注解的类是控制器,@Service注解的是服务层 -->
<context:component-scan base-package="com.cmsz.crm" />
<!--开启注解驱动-->
<mvc:annotation-driven/> <!--避免IE执行AJAX时,返回JSON出现下载文件 --> <bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean> <!-- 启动SpringMVC的注解功能,完成请求和注解POJO的映射 --> <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJacksonHttpMessageConverter" /> <!-- JSON转换器 --> </list> </property> </bean>