解决在spring配置文件中包扫描无效问题

来源:互联网 发布:淘宝我的小蜜是客服么 编辑:程序博客网 时间:2024/05/22 06:04

自己写的一个小项目,用的框架ssm整合,里面明明配置了包扫描,但是就出现了这个异常

org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'adminPersonController': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: private com.drs.service.impl.AdministratorService com.drs.controller.AdminPersonController.administratorService; nested exception is org.springframework.beans.factory.NoSuchBeanDefinitionException: No qualifying bean of type [com.drs.service.impl.AdministratorService] found for dependency: expected at least 1 bean which qualifies as autowire candidate for this dependency. Dependency annotations: {@org.springframework.beans.factory.annotation.Autowired(required=true)}

大概意思bean的创建出了问题,自动注入找不到Service层的东西,首先展示一下我的配置文件

web.xml:

<?xml version="1.0" encoding="UTF-8"?><web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5">  <display-name>diving-school</display-name>  <welcome-file-list>    <welcome-file>index.html</welcome-file>    <welcome-file>index.htm</welcome-file>    <welcome-file>index.jsp</welcome-file>    <welcome-file>default.html</welcome-file>    <welcome-file>default.htm</welcome-file>    <welcome-file>default.jsp</welcome-file>  </welcome-file-list>    <filter>  <filter-name>CharacterEncodingFilter</filter-name>  <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  <init-param>  <param-name>encoding</param-name>  <param-value>utf-8</param-value>  </init-param>  </filter>  <filter-mapping>  <filter-name>CharacterEncodingFilter</filter-name>  <url-pattern>/*</url-pattern>  </filter-mapping>
<!--加载spring容器配置文件-->  <context-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/applicationContext-*.xml</param-value>  </context-param> <!--<listener><listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  </listener>-->  <servlet><servlet-name>springmvc</servlet-name><servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加载springmvc容器-->
<init-param><param-name>contextConfigLocation</param-name><param-value>classpath:spring/springmvc.xml</param-value></init-param>  </servlet>  <servlet-mapping><servlet-name>springmvc</servlet-name><url-pattern>*.action</url-pattern>  </servlet-mapping>    <!-- 消除spring jar包中的bug --> <context-param>      <param-name>spring.profiles.active</param-name>      <param-value>dev</param-value>  </context-param>  <context-param>      <param-name>spring.profiles.default</param-name>      <param-value>dev</param-value>  </context-param><context-param>      <param-name>spring.liveBeansView.mbeanDomain</param-name>      <param-value>dev</param-value>  </context-param>  </web-app>


spring 配置文件:applicationConext-service.xml

<?xml version="1.0" encoding="UTF-8"?><beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd " xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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"><!--扫描service层--><context:component-scan base-package="com.drs.service.impl"></context:component-scan></beans>


springmvc配置文件:springmvc.xml

<?xml version="1.0" encoding="UTF-8"?><beans xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd " xmlns:tx="http://www.springframework.org/schema/tx" xmlns:aop="http://www.springframework.org/schema/aop" 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">
<!--web层包扫描--><context:component-scan base-package="com.drs.controller"/><mvc:annotation-driven conversion-service="conversionService" /><!--<bean id="validator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean"><property name="providerClass" value="org.hibernate.validator.HibernateValidator"/><property name="validationMessageSource" ref="messageSource"/></bean><bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource"><property name="basenames"><list><value>classpath:CustomValidationMessages</value></list></property><property name="fileEncodings" value="utf-8"/><property name="cacheSeconds" value="120"/></bean>--><mvc:resources location="/js/" mapping="/js/**"/><mvc:resources location="/img/" mapping="/img/**"/><bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"><property name="prefix" value="/"/><property name="suffix" value=".jsp"/></bean><!-- 文件上传 --><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><!-- 设置上传文件的最大尺寸为5MB --><property name="maxUploadSize"><value>5242880</value></property></bean><mvc:interceptors><!--多个拦截器,顺序执行 --><!-- 登陆认证拦截器 --><mvc:interceptor><mvc:mapping path="/**"/><bean class="com.drs.controller.interceptor.HandlerInterceptorLogin"></bean></mvc:interceptor></mvc:interceptors><bean id="conversionService" class="org.springframework.format.support.FormattingConversionServiceFactoryBean"><property name="converters"><list><bean class="com.drs.controller.converter.CustomDateConverter"/></list></property></bean><!--<bean class="cn.itcast.ssm.controller.exception.CustomExceptionResolver"></bean><bean id="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver"><property name="maxUploadSize"><value>5242880</value></property></bean><mvc:resources location="/js/" mapping="/js/**"/><mvc:resources location="/img/" mapping="/img/**"/>--><!--不推荐使用<bean class="org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"><property name="interceptors"><list><ref bean="handlerInterceptor1"/><ref bean="handlerInterceptor2"/></list></property></bean><bean id="handlerInterceptor1" class="cn.itcast.ssm.controller.interceptor.HandlerInterceptor1"/><bean id="handlerInterceptor2" class="cn.itcast.ssm.controller.interceptor.HandlerInterceptor2"/>--><!--<mvc:interceptors><mvc:interceptor><mvc:mapping path="//**"/><bean class="cn.itcast.ssm.interceptor.LoginInterceptor"></bean></mvc:interceptor><mvc:interceptor><mvc:mapping path="/**"/><bean class="cn.itcast.ssm.interceptor.HandlerInterceptor1"></bean></mvc:interceptor><mvc:interceptor><mvc:mapping path="/**"/><bean class="cn.itcast.ssm.interceptor.HandlerInterceptor2"></bean></mvc:interceptor></mvc:interceptors>--></beans>

我把service层放到了spring容器中,把web层放到了springmvc容器中,两个容器是父子容器关系,就像java类中继承类似,子类可以访问父类的方法,但父类不能访问子类的方法,同样道理,springmvc容器可以访问spring容器的bean,但spring不可以访问springmvc容器的bean。这里我们在 web层也就是controller自动注入(@Autowired)service层的bean,这个过程肯定需要spring容器访问springmvc容器,但是肯定访问不了,所以web层会抛出异常,service层的bean未创建,所以这些包扫描应该配置到springmvc配置文件上,那么我们使用spring干嘛,一方面spring扩展性比较好,另一方面,管理事务,包括一些aop编程。所以以后需要把service层包扫描配置到springmvc配置文件中。



阅读全文
1 0
原创粉丝点击