相同Bean分別被Spring MVC子容器與Spring父容器初始化,致使@Value注入失敗

#1 問題描述# 在車保養項目開發過程當中,技術架構:Spring MVC + MyBatis;Service層接口中屬性,若是使用註解@Value注入,不可以拿到Properties文件中拿到對應的key值;但在Spring配置文件applicationContext-xxx.xml文件中配置的Properties就能夠拿到。具體項目中相關代碼以下:html

  1. 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">

    <!-- 自定義的參數解析器放在第一位置 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <!-- 自定義參數解析器 -->
        <property name="customArgumentResolvers">
            <list>
                <bean class="com.qding.base.resolver.ArgumentFromJsonResolver" />
            </list>
        </property>
    </bean>

    <!-- 開啓組件掃描 -->
    <!-- 對包中的全部類進行掃描,以完成Bean建立和自動依賴注入的功能 -->
    <context:component-scan base-package="com.qding"/>

    <!-- 開啓註解 -->
    <mvc:annotation-driven />

    <!-- 啓用AspectJ對Annotation的支持 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- 靜態資源路徑 -->
    <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>

    <!-- 配置多請求數據類型,如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方法參數校驗 -->
    <bean class="com.qding.base.aspect.ParameterValidateAspect" />
</beans>
  1. 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建立和自動依賴注入的功能 -->
    <context:component-scan base-package="com.qding.*.*.service"/>

    <!-- 定時任務 -->
    <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>

    <!--把事務控制在Service層-->
    <aop:config>
        <aop:pointcut id="pc" expression="execution(public * com.qding..service.*.*(..))" />
        <aop:advisor pointcut-ref="pc" advice-ref="useTxAdvice" />
    </aop:config>

    <!-- 切面配置:Service層方法執行日誌 -->
    <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>
  1. 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 排查過程#java

  1. Spring的applicationContext-service.xml文件配置的屬性,能夠正常拿到Properties文件中的值;【正常】
  2. 項目工程的Service層OrderServiceImpl實現,@Value不能拿到Properties文件中的值;【不正常】
  3. 代碼斷點調試:發現OrderServiceImpl被初始化了兩次,第一次@Value能夠拿到值,第二次@Value沒有拿到值;【不正常】
  4. 發現根本緣由:Spring 容器和Spring MVC容器分別都初始化了Service的實例,後者第二次初始化Service實例時,沒有拿到@Value值,該實例覆蓋掉了Spring 容器初始化的實例;

#3 解決方案# 經過修改兩個配置文件的<context:component-scan base-package=""/>掃包範圍,達到如下效果:web

  1. Spring MVC的配置文件dispatcher-servlet嚴格限制只初始化Controller層實例;
  2. Spring的配置文件applicationContext-service.xml嚴格限制只初始化除Controller層的其餘層實例;

修改後的配置文件:spring

  1. 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">

    <!-- 自定義的參數解析器放在第一位置 -->
    <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">
        <!-- 自定義參數解析器 -->
        <property name="customArgumentResolvers">
            <list>
                <bean class="com.qding.base.resolver.ArgumentFromJsonResolver" />
            </list>
        </property>
    </bean>

    <!-- 開啓組件掃描 -->
    <!-- 對包中的全部類進行掃描,以完成Bean建立和自動依賴注入的功能 -->
    <context:component-scan base-package="com.qding.*.*.controller,com.qding.*.controller"/>

    <!-- 開啓註解 -->
    <mvc:annotation-driven />

    <!-- 啓用AspectJ對Annotation的支持 -->
    <aop:aspectj-autoproxy proxy-target-class="true"/>

    <!-- 靜態資源路徑 -->
    <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>

    <!-- 配置多請求數據類型,如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方法參數校驗 -->
    <bean class="com.qding.base.aspect.ParameterValidateAspect" />
</beans>
  1. 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建立和自動依賴注入的功能 -->
    <context:component-scan base-package="com.qding.*.*.service,com.qding.*.*.imessage,com.qding.*.quartz,com.qding.remote.service"/>

    <!-- 定時任務 -->
    <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>

    <!--把事務控制在Service層-->
    <aop:config>
        <aop:pointcut id="pc" expression="execution(public * com.qding..service.*.*(..))" />
        <aop:advisor pointcut-ref="pc" advice-ref="useTxAdvice" />
    </aop:config>

    <!-- 切面配置:Service層方法執行日誌 -->
    <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 問題總結# SpringMVC容器是Spring容器的一個子容器,它一樣可以初始化實體類。因爲SpringMVC容器的初始化是在Spring容器初始化以後,因此它會替換Spring中已經存在的類,這樣可能會致使衝突。所以在Spring的配置文件中SpringMVC和Spring容器各司其職,在使用ComponentScan進行掃描時,各自掃描各自的實體類。以下配置:express

  1. Spring容器掃描配置
<context:component-scan base-package="com.projects.system">
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller" />         
    </context:component-scan>
  1. 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掃描配置中要將其過濾掉,不然會致使SpringMVC 的rest地址不可訪問的問題。新的配置以下:json

<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:exclude-filter type="annotation" expression="org.springframework.context.annotation.Configuration"/>
    </context:component-scan>
相關文章
相關標籤/搜索