美文网首页邦仁聊技术首页投稿(暂停使用,暂停投稿)程序员
☆相同Bean分别被Spring MVC子容器与Spring父容

☆相同Bean分别被Spring MVC子容器与Spring父容

作者: 七寸知架构 | 来源:发表于2016-12-16 10:08 被阅读948次

    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>
    

    相关文章

      网友评论

        本文标题:☆相同Bean分别被Spring MVC子容器与Spring父容

        本文链接:https://www.haomeiwen.com/subject/cbknmttx.html