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

1 問題描述

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

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>

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>

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 排查過程

  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=""/>掃包范圍,達到以下效果:

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

修改后的配置文件:

  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 問題總結

Spring MVC容器是Spring容器的一個子容器,它同樣能夠初始化實體類。由于SpringMVC容器的初始化是在Spring容器初始化之后,所以它會替換Spring中已經存在的類,這樣可能會導致沖突。因此在Spring的配置文件中SpringMVC和Spring容器各司其職,在使用ComponentScan進行掃描時,各自掃描各自的實體類。如下配置:

  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地址不可訪問的問題。新的配置如下:

    <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>
最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容