您的位置:首页 > 编程语言 > Java开发

相同Bean分别被Spring MVC子容器与Spring父容器初始化,导致@Value注入失败

2017-01-13 14:38 861 查看

1问题描述

在车保养项目开发过程中,技术架构:SpringMVC+MyBatis;Service层接口中属性,如果使用注解@Value注入,不能够拿到Properties文件中拿到对应的key值;但在Spring配置文件applicationContext-xxx.xml文件中配置的Properties就可以拿到。具体项目中相关代码如下:

SpringMVC的dispatcher-servlet.xml文件:

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">
<!--自定义的参数解析器放在第一位置-->
<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<!--自定义参数解析器-->
<propertyname="customArgumentResolvers">
<list>
<beanclass="com.qding.base.resolver.ArgumentFromJsonResolver"/>
</list>
</property>
</bean>

<!--开启组件扫描-->
<!--对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能-->
<context:component-scanbase-package="com.qding"/>

<!--开启注解-->
<mvc:annotation-driven/>

<!--启用AspectJ对Annotation的支持-->
<aop:aspectj-autoproxyproxy-target-class="true"/>

<!--静态资源路径-->
<mvc:resourceslocation="/easyui/"mapping="/easyui/**"/>
<mvc:resourceslocation="/js/"mapping="/js/**"/>
<mvc:resourceslocation="/html/"mapping="/html/**"/>

<beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="viewClass"value="org.springframework.web.servlet.view.JstlView"/>
<propertyname="prefix"value="/WEB-INF/views/"/>
<propertyname="suffix"value=".jsp"/>
</bean>

<!--配置多请求数据类型,如jsonxml-->
<beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--setthemaxuploadsize10MB-->
<propertyname="defaultEncoding"value="UTF-8"/>
<propertyname="maxUploadSize"value="10485760"/>
<propertyname="maxInMemorySize"value="10240"/>
</bean>

<!--配置Controller拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mappingpath="/**"/>
<mvc:exclude-mappingpath="/remote/imessage"/>
<mvc:exclude-mappingpath="/easyui/**"/>
<mvc:exclude-mappingpath="/js/**"/>
<mvc:exclude-mappingpath="/html/**"/>
<beanclass="com.qding.base.interceptor.TransferSecurityInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mappingpath="/**"/>
<mvc:exclude-mappingpath="/remote/imessage"/>
<mvc:exclude-mappingpath="/easyui/**"/>
<mvc:exclude-mappingpath="/js/**"/>
<mvc:exclude-mappingpath="/html/**"/>
<beanclass="com.qding.doc.interceptor.TransferDocInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

<!--切面配置:Controller方法参数校验-->
<beanclass="com.qding.base.aspect.ParameterValidateAspect"/>
</beans>


Spring的applicationContext-service.xml文件:

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启组件扫描-->
<!--对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能-->
<context:component-scanbase-package="com.qding.*.*.service"/>

<!--定时任务-->
<task:annotation-driven/>

<!--启用AspectJ对Annotation的支持-->
<aop:aspectj-autoproxy/>

<!--TransactionSupport-->
<tx:adviceid="useTxAdvice"transaction-manager="txManager">
<tx:attributes>
<tx:methodname="*remove*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="*save*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="*modify*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="*update*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="create*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="fill*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="cancel*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="*chang*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>

<tx:methodname="find*"propagation="SUPPORTS"/>
<tx:methodname="get*"propagation="SUPPORTS"/>
<tx:methodname="query*"propagation="SUPPORTS"/>
<tx:methodname="page*"propagation="SUPPORTS"/>
<tx:methodname="count*"propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>

<!--把事务控制在Service层-->
<aop:config>
<aop:pointcutid="pc"expression="execution(public*com.qding..service.*.*(..))"/>
<aop:advisorpointcut-ref="pc"advice-ref="useTxAdvice"/>
</aop:config>

<!--切面配置:Service层方法执行日志-->
<beanclass="com.qding.aspect.ServiceVersionLogAspect"/>

<!--memcached客户端配置-->
<beanname="xmemcachedClientBuilder"class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
<constructor-arg>
<list>
<beanclass="java.net.InetSocketAddress">
<constructor-arg>
<value>${server_1
4000
}</value>
</constructor-arg>
<constructor-arg>
<value>${port_1}</value>
</constructor-arg>
</bean>
</list>
</constructor-arg>
<constructor-arg>
<list>
<value>${priority_1}</value>
</list>
</constructor-arg>
<propertyname="connectionPoolSize"value="6"/>
<propertyname="commandFactory">
<beanclass="net.rubyeye.xmemcached.command.BinaryCommandFactory"/>
</property>
<propertyname="sessionLocator">
<beanclass="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"/>
</property>
<propertyname="transcoder">
<beanclass="net.rubyeye.xmemcached.transcoders.SerializingTranscoder"/>
</property>
</bean>

<beanname="xmemcachedClient"factory-bean="xmemcachedClientBuilder"factory-method="build"destroy-method="shutdown">
<propertyname="opTimeout"value="3000"/>
</bean>

<beanid="memCacheUtil"class="com.qding.member.common.cache.MemCacheUtil">
<!--过期时间单位秒-->
<propertyname="expTime"value="3600"/>
<!--操作失效时间单位毫秒-->
<propertyname="opTime"value="3000"/>
<propertyname="memcachedClient"ref="xmemcachedClient"/>
</bean>
</beans>


Service层OrderServiceImpl的代码:

publicclassOrderServiceImplimplementsOrderService{

@Value("${bopai.provider_id}")
privateStringbopaiProviderId;

@Value("${bopai.provider_name}")
privateStringbopaiProviderName;

@Value("${bopai.connect.phone}")
privateStringboPaiPhone;

......
}


2排查过程

Spring的applicationContext-service.xml文件配置的属性,可以正常拿到Properties文件中的值;【正常】
项目工程的Service层OrderServiceImpl实现,@Value不能拿到Properties文件中的值;【不正常】
代码断点调试:发现OrderServiceImpl被初始化了两次,第一次@Value可以拿到值,第二次@Value没有拿到值;【不正常】
发现根本原因:Spring容器和SpringMVC容器分别都初始化了Service的实例,后者第二次初始化Service实例时,没有拿到@Value值,该实例覆盖掉了Spring容器初始化的实例;

3解决方案

通过修改两个配置文件的
<context:component-scanbase-package=""/>
扫包范围,达到以下效果:

SpringMVC的配置文件dispatcher-servlet严格限制只初始化Controller层实例;
Spring的配置文件applicationContext-service.xml严格限制只初始化除Controller层的其他层实例;
修改后的配置文件:

SpringMVC的dispatcher-servlet.xml文件:

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/mvchttp://www.springframework.org/schema/mvc/spring-mvc.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd">
<!--自定义的参数解析器放在第一位置-->
<beanclass="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<!--自定义参数解析器-->
<propertyname="customArgumentResolvers">
<list>
<beanclass="com.qding.base.resolver.ArgumentFromJsonResolver"/>
</list>
</property>
</bean>

<!--开启组件扫描-->
<!--对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能-->
<context:component-scanbase-package="com.qding.*.*.controller,com.qding.*.controller"/>

<!--开启注解-->
<mvc:annotation-driven/>

<!--启用AspectJ对Annotation的支持-->
<aop:aspectj-autoproxyproxy-target-class="true"/>

<!--静态资源路径-->
<mvc:resourceslocation="/easyui/"mapping="/easyui/**"/>
<mvc:resourceslocation="/js/"mapping="/js/**"/>
<mvc:resourceslocation="/html/"mapping="/html/**"/>

<beanid="viewResolver"class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<propertyname="viewClass"value="org.springframework.web.servlet.view.JstlView"/>
<propertyname="prefix"value="/WEB-INF/views/"/>
<propertyname="suffix"value=".jsp"/>
</bean>

<!--配置多请求数据类型,如jsonxml-->
<beanid="multipartResolver"class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!--setthemaxuploadsize10MB-->
<propertyname="defaultEncoding"value="UTF-8"/>
<propertyname="maxUploadSize"value="10485760"/>
<propertyname="maxInMemorySize"value="10240"/>
</bean>

<!--配置Controller拦截器-->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mappingpath="/**"/>
<mvc:exclude-mappingpath="/remote/imessage"/>
<mvc:exclude-mappingpath="/easyui/**"/>
<mvc:exclude-mappingpath="/js/**"/>
<mvc:exclude-mappingpath="/html/**"/>
<beanclass="com.qding.base.interceptor.TransferSecurityInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mappingpath="/**"/>
<mvc:exclude-mappingpath="/remote/imessage"/>
<mvc:exclude-mappingpath="/easyui/**"/>
<mvc:exclude-mappingpath="/js/**"/>
<mvc:exclude-mappingpath="/html/**"/>
<beanclass="com.qding.doc.interceptor.TransferDocInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

<!--切面配置:Controller方法参数校验-->
<beanclass="com.qding.base.aspect.ParameterValidateAspect"/>
</beans>


Spring的applicationContext-service.xml文件:

<?xmlversion="1.0"encoding="UTF-8"?>
<beansxmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:task="http://www.springframework.org/schema/task"
xmlns:aop="http://www.springframework.org/schema/aop"xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans-3.0.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context-3.0.xsdhttp://www.springframework.org/schema/taskhttp://www.springframework.org/schema/task/spring-task.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsdhttp://www.springframework.org/schema/txhttp://www.springframework.org/schema/tx/spring-tx.xsd">
<!--开启组件扫描-->
<!--对包中的所有类进行扫描,以完成Bean创建和自动依赖注入的功能-->
<context:component-scanbase-package="com.qding.*.*.service,com.qding.*.*.imessage,com.qding.*.quartz,com.qding.remote.service"/>

<!--定时任务-->
<task:annotation-driven/>

<!--启用AspectJ对Annotation的支持-->
<aop:aspectj-autoproxy/>

<!--TransactionSupport-->
<tx:adviceid="useTxAdvice"transaction-manager="txManager">
<tx:attributes>
<tx:methodname="*remove*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="*save*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="*modify*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="*update*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="create*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="fill*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="cancel*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>
<tx:methodname="*chang*"propagation="REQUIRED"read-only="false"rollback-for="java.lang.Exception"/>

<tx:methodname="find*"propagation="SUPPORTS"/>
<tx:methodname="get*"propagation="SUPPORTS"/>
<tx:methodname="query*"propagation="SUPPORTS"/>
<tx:methodname="page*"propagation="SUPPORTS"/>
<tx:methodname="count*"propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>

<!--把事务控制在Service层-->
<aop:config>
<aop:pointcutid="pc"expression="execution(public*com.qding..service.*.*(..))"/>
<aop:advisorpointcut-ref="pc"advice-ref="useTxAdvice"/>
</aop:config>

<!--切面配置:Service层方法执行日志-->
<beanclass="com.qding.aspect.ServiceVersionLogAspect"/>

<!--memcached客户端配置-->
<beanname="xmemcachedClientBuilder"class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
<constructor-arg>
<list>
<beanclass="java.net.InetSocketAddress">
<constructor-arg>
<value>${server_1}</value>
</constructor-arg>
<constructor-arg>
<value>${port_1}</value>
</constructor-arg>
</bean>
</list>
</constructor-arg>
<constructor-arg>
<list>
<value>${priority_1}</value>
</list>
</constructor-arg>
<propertyname="connectionPoolSize"value="6"/>
<propertyname="commandFactory">
<beanclass="net.rubyeye.xmemcached.command.BinaryCommandFactory"/>
</property>
<propertyname="sessionLocator">
<beanclass="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"/>
</property>
<propertyname="transcoder">
<beanclass="net.rubyeye.xmemcached.transcoders.SerializingTranscoder"/>
</property>
</bean>

<beanname="xmemcachedClient"factory-bean="xmemcachedClientBuilder"factory-method="build"destroy-method="shutdown">
<propertyname="opTimeout"value="3000"/>
</bean>

<beanid="memCacheUtil"class="com.qding.member.common.cache.MemCacheUtil">
<!--过期时间单位秒-->
<propertyname="expTime"value="3600"/>
<!--操作失效时间单位毫秒-->
<propertyname="opTime"value="3000"/>
<propertyname="memcachedClient"ref="xmemcachedClient"/>
</bean>
</beans>


4问题总结

SpringMVC容器是Spring容器的一个子容器,它同样能够初始化实体类。
由于SpringMVC容器的初始化是在Spring容器初始化之后,所以它会替换Spring中已经存在的类,这样可能会导致冲突
。因此在Spring的配置文件中SpringMVC和Spring容器各司其职,在使用ComponentScan进行扫描时,各自扫描各自的实体类。如下配置:

Spring容器扫描配置:

<context:component-scanbase-package="com.projects.system">
<context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Controller"/>
</context:component-scan>


SpringMVC容器扫描配置:

<context:component-scanbase-package="com.projects.system">
<context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Service"/>
<context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Repository"/>
</context:component-scan>


以上配置在使用Springxml-based配置时是没有问题的。如果在项目中引入java-base配置时,同时引入了@Configuration注解,
@Configuration注解是在Spring容器初始化时进行实体类的初始化工作
,因此在SpringMVC扫描配置中要将其过滤掉,否则会导致SpringMVC的rest地址不可访问的问题。新的配置如下:

<context:component-scanbase-package="com.projects.system">
<context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Service"/>
<context:exclude-filtertype="annotation"expression="org.springframework.stereotype.Repository"/>
<!--不扫描配置文件类,避免重复初始化-->
<context:exclude-filtertype="annotation"expression="org.springframework.context.annotation.Configuration"/>
</context:component-scan>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息