level 1
组件大厦
楼主
最近开始学习spring和springMVC整合,写了个小项目测试可是一直报错,还找不到原因,希望各位大神可以帮帮忙,错误信息太多,只粘关键部分了
Caused by:org.springframework.beans.factory.BeanCreationException: Could not autowirefield: com.service.IUserService com.controller.UserController.userService;nested exception isorg.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifyingbean of type [com.service.IUserService] found for dependency: expected at least1 bean which qualifies as autowire candidate for this dependency. Dependencyannotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
... 36 more
Caused by:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifyingbean of type [com.service.IUserService] found for dependency: expected at least1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
atorg.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
atorg.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
... 38 more
接下来是配置文件spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- Config: Scan Package -->
<context:component-scan base-package="com.controller"use-default-filters="false">
<context:include-filtertype="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- Config: Annotation Driven -->
<mvc:annotation-driven/>
<!-- Config: Default Servlet Handler -->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!-- Config: Resource View Resolver -->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"value="/page/"></property>
<property name="suffix"value=".jsp"></property>
</bean>
<!-- Config: JSON & String Converter -->
<bean id="stringConverter"class="org.springframework.http.converter.StringHttpMessageConverter">
<propertyname="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="jsonConverter"class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<!--Handler Adapter-->
<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<refbean="stringConverter"/>
<refbean="jsonConverter"/>
</list>
</property>
</bean>
<!-- Config: Local parser based on Cookie -->
<bean id="localeResolver"class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieMaxAge"value="604800"/>
<property name="defaultLocale"value="en"/>
<property name="cookieName"value="Language"></property>
</bean>
<!-- Config: Filter specified URL -->
<!--
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mappingpath="/demo/**"></mvc:exclude-mapping>
<mvc:exclude-mappingpath="/admin/demo/**"></mvc:exclude-mapping>
<mvc:exclude-mappingpath="/staticmedia/**"></mvc:exclude-mapping>
<mvc:exclude-mappingpath="/test"></mvc:exclude-mapping>
<beanclass="com.company.project.common.util.Interceptor.LoggedInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
-->
</beans>
还有applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
<!-- Config: Scan Package -->
<context:component-scan base-package="com">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!--开启注解处理器-->
<context:annotation-config/>
<!-- Config: Support to upload files -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding"value="UTF-8"></property>
</bean>
<!-- Config: Configure BeanPostProcessor -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>
2017年04月27日 09点04分
1
Caused by:org.springframework.beans.factory.BeanCreationException: Could not autowirefield: com.service.IUserService com.controller.UserController.userService;nested exception isorg.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifyingbean of type [com.service.IUserService] found for dependency: expected at least1 bean which qualifies as autowire candidate for this dependency. Dependencyannotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:514)
at org.springframework.beans.factory.annotation.InjectionMetadata.inject(InjectionMetadata.java:87)
atorg.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:285)
... 36 more
Caused by:org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifyingbean of type [com.service.IUserService] found for dependency: expected at least1 bean which qualifies as autowire candidate for this dependency. Dependency annotations:{@org.springframework.beans.factory.annotation.Autowired(required=true)}
atorg.springframework.beans.factory.support.DefaultListableBeanFactory.raiseNoSuchBeanDefinitionException(DefaultListableBeanFactory.java:988)
at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:858)
atorg.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:770)
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor$AutowiredFieldElement.inject(AutowiredAnnotationBeanPostProcessor.java:486)
... 38 more
接下来是配置文件spring-mvc.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">
<!-- Config: Scan Package -->
<context:component-scan base-package="com.controller"use-default-filters="false">
<context:include-filtertype="annotation"
expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- Config: Annotation Driven -->
<mvc:annotation-driven/>
<!-- Config: Default Servlet Handler -->
<mvc:default-servlet-handler></mvc:default-servlet-handler>
<!-- Config: Resource View Resolver -->
<beanclass="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix"value="/page/"></property>
<property name="suffix"value=".jsp"></property>
</bean>
<!-- Config: JSON & String Converter -->
<bean id="stringConverter"class="org.springframework.http.converter.StringHttpMessageConverter">
<propertyname="supportedMediaTypes">
<list>
<value>text/html;charset=UTF-8</value>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
<bean id="jsonConverter"class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"></bean>
<!--Handler Adapter-->
<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<property name="messageConverters">
<list>
<refbean="stringConverter"/>
<refbean="jsonConverter"/>
</list>
</property>
</bean>
<!-- Config: Local parser based on Cookie -->
<bean id="localeResolver"class="org.springframework.web.servlet.i18n.CookieLocaleResolver">
<property name="cookieMaxAge"value="604800"/>
<property name="defaultLocale"value="en"/>
<property name="cookieName"value="Language"></property>
</bean>
<!-- Config: Filter specified URL -->
<!--
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<mvc:exclude-mappingpath="/demo/**"></mvc:exclude-mapping>
<mvc:exclude-mappingpath="/admin/demo/**"></mvc:exclude-mapping>
<mvc:exclude-mappingpath="/staticmedia/**"></mvc:exclude-mapping>
<mvc:exclude-mappingpath="/test"></mvc:exclude-mapping>
<beanclass="com.company.project.common.util.Interceptor.LoggedInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
-->
</beans>
还有applicationContext.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:mongo="http://www.springframework.org/schema/data/mongo"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/data/mongo
http://www.springframework.org/schema/data/mongo/spring-mongo.xsd">
<!-- Config: Scan Package -->
<context:component-scan base-package="com">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<!--开启注解处理器-->
<context:annotation-config/>
<!-- Config: Support to upload files -->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<property name="defaultEncoding"value="UTF-8"></property>
</bean>
<!-- Config: Configure BeanPostProcessor -->
<bean class="org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor"/>
</beans>