1 問題描述
在車保養(yǎng)項目開發(fā)過程中隙咸,技術(shù)架構(gòu):Spring MVC + MyBatis;Service層接口中屬性掖疮,如果使用注解@Value注入初茶,不能夠拿到Properties文件中拿到對應(yīng)的key值;但在Spring配置文件applicationContext-xxx.xml文件中配置的Properties就可以拿到浊闪。具體項目中相關(guān)代碼如下:
Spring MVC的dispatcher-servlet.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 自定義的參數(shù)解析器放在第一位置 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<!-- 自定義參數(shù)解析器 -->
<property name="customArgumentResolvers">
<list>
<bean class="com.qding.base.resolver.ArgumentFromJsonResolver" />
</list>
</property>
</bean>
<!-- 開啟組件掃描 -->
<!-- 對包中的所有類進行掃描恼布,以完成Bean創(chuàng)建和自動依賴注入的功能 -->
<context:component-scan base-package="com.qding"/>
<!-- 開啟注解 -->
<mvc:annotation-driven />
<!-- 啟用AspectJ對Annotation的支持 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 靜態(tài)資源路徑 -->
<mvc:resources location="/easyui/" mapping="/easyui/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/html/" mapping="/html/**"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置多請求數(shù)據(jù)類型,如json xml-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set the max upload size10MB -->
<property name="defaultEncoding" value="UTF-8" />
<property name="maxUploadSize" value="10485760" />
<property name="maxInMemorySize" value="10240" />
</bean>
<!-- 配置Controller攔截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/remote/imessage"/>
<mvc:exclude-mapping path="/easyui/**"/>
<mvc:exclude-mapping path="/js/**"/>
<mvc:exclude-mapping path="/html/**"/>
<bean class="com.qding.base.interceptor.TransferSecurityInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/remote/imessage"/>
<mvc:exclude-mapping path="/easyui/**"/>
<mvc:exclude-mapping path="/js/**"/>
<mvc:exclude-mapping path="/html/**"/>
<bean class="com.qding.doc.interceptor.TransferDocInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<!-- 切面配置:Controller方法參數(shù)校驗 -->
<bean class="com.qding.base.aspect.ParameterValidateAspect" />
</beans>
Spring的applicationContext-service.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/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-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 開啟組件掃描 -->
<!-- 對包中的所有類進行掃描搁宾,以完成Bean創(chuàng)建和自動依賴注入的功能 -->
<context:component-scan base-package="com.qding.*.*.service"/>
<!-- 定時任務(wù) -->
<task:annotation-driven/>
<!-- 啟用AspectJ對Annotation的支持 -->
<aop:aspectj-autoproxy/>
<!-- Transaction Support -->
<tx:advice id="useTxAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*remove*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="*save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="*modify*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="*update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="create*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="fill*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="cancel*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="*chang*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="find*" propagation="SUPPORTS"/>
<tx:method name="get*" propagation="SUPPORTS"/>
<tx:method name="query*" propagation="SUPPORTS"/>
<tx:method name="page*" propagation="SUPPORTS"/>
<tx:method name="count*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!--把事務(wù)控制在Service層-->
<aop:config>
<aop:pointcut id="pc" expression="execution(public * com.qding..service.*.*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="useTxAdvice" />
</aop:config>
<!-- 切面配置:Service層方法執(zhí)行日志 -->
<bean class="com.qding.aspect.ServiceVersionLogAspect" />
<!--memcached客戶端配置-->
<bean name="xmemcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
<constructor-arg>
<list>
<bean class="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>
<property name="connectionPoolSize" value="6"/>
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"/>
</property>
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"/>
</property>
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
</bean>
<bean name="xmemcachedClient" factory-bean="xmemcachedClientBuilder" factory-method="build" destroy-method="shutdown">
<property name="opTimeout" value="3000"/>
</bean>
<bean id="memCacheUtil" class="com.qding.member.common.cache.MemCacheUtil">
<!-- 過期時間 單位秒 -->
<property name="expTime" value="3600"/>
<!-- 操作失效時間 單位毫秒 -->
<property name="opTime" value="3000"/>
<property name="memcachedClient" ref="xmemcachedClient"/>
</bean>
</beans>
Service層OrderServiceImpl的代碼:
public class OrderServiceImpl implements OrderService {
@Value("${bopai.provider_id}")
private String bopaiProviderId;
@Value("${bopai.provider_name}")
private String bopaiProviderName;
@Value("${bopai.connect.phone}")
private String boPaiPhone;
......
}
2 排查過程
- Spring的applicationContext-service.xml文件配置的屬性折汞,可以正常拿到Properties文件中的值;【正掣峭龋】
- 項目工程的Service層OrderServiceImpl實現(xiàn)爽待,@Value不能拿到Properties文件中的值损同;【不正常】
- 代碼斷點調(diào)試:
發(fā)現(xiàn)OrderServiceImpl被初始化了兩次鸟款,第一次@Value可以拿到值膏燃,第二次@Value沒有拿到值;
【不正澈问玻】 - 發(fā)現(xiàn)根本原因:
Spring 容器和Spring MVC容器分別都初始化了Service的實例组哩,后者第二次初始化Service實例時,沒有拿到@Value值处渣,該實例覆蓋掉了Spring 容器初始化的實例伶贰;
3 解決方案
通過修改兩個配置文件的<context:component-scan base-package=""/>
掃包范圍,達到以下效果:
- Spring MVC的配置文件dispatcher-servlet嚴格限制只初始化Controller層實例罐栈;
- Spring的配置文件applicationContext-service.xml嚴格限制只初始化除Controller層的其他層實例黍衙;
修改后的配置文件:
- Spring MVC的dispatcher-servlet.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd">
<!-- 自定義的參數(shù)解析器放在第一位置 -->
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
<!-- 自定義參數(shù)解析器 -->
<property name="customArgumentResolvers">
<list>
<bean class="com.qding.base.resolver.ArgumentFromJsonResolver" />
</list>
</property>
</bean>
<!-- 開啟組件掃描 -->
<!-- 對包中的所有類進行掃描,以完成Bean創(chuàng)建和自動依賴注入的功能 -->
<context:component-scan base-package="com.qding.*.*.controller,com.qding.*.controller"/>
<!-- 開啟注解 -->
<mvc:annotation-driven />
<!-- 啟用AspectJ對Annotation的支持 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>
<!-- 靜態(tài)資源路徑 -->
<mvc:resources location="/easyui/" mapping="/easyui/**"/>
<mvc:resources location="/js/" mapping="/js/**"/>
<mvc:resources location="/html/" mapping="/html/**"/>
<bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="viewClass" value="org.springframework.web.servlet.view.JstlView"/>
<property name="prefix" value="/WEB-INF/views/"/>
<property name="suffix" value=".jsp" />
</bean>
<!-- 配置多請求數(shù)據(jù)類型荠诬,如json xml-->
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- set the max upload size10MB -->
<property name="defaultEncoding" value="UTF-8" />
<property name="maxUploadSize" value="10485760" />
<property name="maxInMemorySize" value="10240" />
</bean>
<!-- 配置Controller攔截器 -->
<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/remote/imessage"/>
<mvc:exclude-mapping path="/easyui/**"/>
<mvc:exclude-mapping path="/js/**"/>
<mvc:exclude-mapping path="/html/**"/>
<bean class="com.qding.base.interceptor.TransferSecurityInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<mvc:mapping path="/**" />
<mvc:exclude-mapping path="/remote/imessage"/>
<mvc:exclude-mapping path="/easyui/**"/>
<mvc:exclude-mapping path="/js/**"/>
<mvc:exclude-mapping path="/html/**"/>
<bean class="com.qding.doc.interceptor.TransferDocInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>
<!-- 切面配置:Controller方法參數(shù)校驗 -->
<bean class="com.qding.base.aspect.ParameterValidateAspect" />
</beans>
- Spring的applicationContext-service.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="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/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-3.0.xsd
http://www.springframework.org/schema/task
http://www.springframework.org/schema/task/spring-task.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx.xsd">
<!-- 開啟組件掃描 -->
<!-- 對包中的所有類進行掃描琅翻,以完成Bean創(chuàng)建和自動依賴注入的功能 -->
<context:component-scan base-package="com.qding.*.*.service,com.qding.*.*.imessage,com.qding.*.quartz,com.qding.remote.service"/>
<!-- 定時任務(wù) -->
<task:annotation-driven/>
<!-- 啟用AspectJ對Annotation的支持 -->
<aop:aspectj-autoproxy/>
<!-- Transaction Support -->
<tx:advice id="useTxAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*remove*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception"/>
<tx:method name="*save*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="*modify*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="*update*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="create*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="fill*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="cancel*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="*chang*" propagation="REQUIRED" read-only="false" rollback-for="java.lang.Exception" />
<tx:method name="find*" propagation="SUPPORTS"/>
<tx:method name="get*" propagation="SUPPORTS"/>
<tx:method name="query*" propagation="SUPPORTS"/>
<tx:method name="page*" propagation="SUPPORTS"/>
<tx:method name="count*" propagation="SUPPORTS"/>
</tx:attributes>
</tx:advice>
<!--把事務(wù)控制在Service層-->
<aop:config>
<aop:pointcut id="pc" expression="execution(public * com.qding..service.*.*(..))" />
<aop:advisor pointcut-ref="pc" advice-ref="useTxAdvice" />
</aop:config>
<!-- 切面配置:Service層方法執(zhí)行日志 -->
<bean class="com.qding.aspect.ServiceVersionLogAspect" />
<!--memcached客戶端配置-->
<bean name="xmemcachedClientBuilder" class="net.rubyeye.xmemcached.XMemcachedClientBuilder">
<constructor-arg>
<list>
<bean class="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>
<property name="connectionPoolSize" value="6"/>
<property name="commandFactory">
<bean class="net.rubyeye.xmemcached.command.BinaryCommandFactory"/>
</property>
<property name="sessionLocator">
<bean class="net.rubyeye.xmemcached.impl.KetamaMemcachedSessionLocator"/>
</property>
<property name="transcoder">
<bean class="net.rubyeye.xmemcached.transcoders.SerializingTranscoder" />
</property>
</bean>
<bean name="xmemcachedClient" factory-bean="xmemcachedClientBuilder" factory-method="build" destroy-method="shutdown">
<property name="opTimeout" value="3000"/>
</bean>
<bean id="memCacheUtil" class="com.qding.member.common.cache.MemCacheUtil">
<!-- 過期時間 單位秒 -->
<property name="expTime" value="3600"/>
<!-- 操作失效時間 單位毫秒 -->
<property name="opTime" value="3000"/>
<property name="memcachedClient" ref="xmemcachedClient"/>
</bean>
</beans>
4 問題總結(jié)
Spring MVC容器是Spring容器的一個子容器,它同樣能夠初始化實體類浅妆。由于SpringMVC容器的初始化是在Spring容器初始化之后望迎,所以它會替換Spring中已經(jīng)存在的類,這樣可能會導(dǎo)致沖突凌外。因此在Spring的配置文件中SpringMVC和Spring容器各司其職辩尊,在使用ComponentScan進行掃描時,各自掃描各自的實體類康辑。如下配置:
- Spring容器掃描配置:
<context:component-scan base-package="com.projects.system">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />
</context:component-scan>
- SpringMVC容器掃描配置:
<context:component-scan base-package="com.projects.system">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
</context:component-scan>
以上配置在使用Spring xml-based配置時是沒有問題的摄欲。如果在項目中引入java-base配置時,同時引入了@Configuration注解疮薇,@Configuration注解是在Spring容器初始化時進行實體類的初始化工作
胸墙,因此在Spring MVC掃描配置中要將其過濾掉,否則會導(dǎo)致SpringMVC 的rest地址不可訪問的問題按咒。新的配置如下:
<context:component-scan base-package="com.projects.system">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Repository"/>
<!-- 不掃描配置文件類迟隅,避免重復(fù)初始化 -->
<context:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>
</context:component-scan>