007-2021 SSM整合后声明式事务失效的两种解决方案

SSM整合配置文件内容如下 : 

1.applicationContext.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: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.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
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx.xsd">
    <!--开启注解扫描 : 注意属性 base-package 包的范围-->
    <context:component-scan base-package="com.msr">
        <!--不要扫描哪个注解-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!-- ===============spring整合MyBatis框架===============-->
    <!--配置链接池-->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="driverClass" value="com.mysql.cj.jdbc.Driver"/>
        <property name="jdbcUrl" value="jdbc:mysql:///ssm?serverTimezone=UTC"/>
        <property name="user" value="root"/>
        <property name="password" value="123"/>
    </bean>
    <!--配置SQLSessionFactory工厂对象-->
    <bean id="sqlSessionFactory" class="org.mybatis.spring.SqlSessionFactoryBean">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置被代理的接口所在的包-->
    <bean id="mapperScannerConfigurer" class="org.mybatis.spring.mapper.MapperScannerConfigurer">
        <property name="basePackage" value="com.msr.dao"/>
    </bean>
    <!--=====================spring声明式事务管理=============================-->
    <!--配置事务管理器-->
    <bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"/>
    </bean>
    <!--配置事务通知-->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <tx:method name="find*" read-only="true" propagation="SUPPORTS"/>
            <tx:method name="*" isolation="DEFAULT" propagation="REQUIRED"/>
        </tx:attributes>
    </tx:advice>
    <!--配置AOP增强-->
    <aop:config>
        <aop:pointcut id="pc1" expression="execution(* com.msr.service.impl.*.*(..))"/>
        <aop:advisor advice-ref="txAdvice" pointcut-ref="pc1"/>
    </aop:config>
</beans>

2.springmvc.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:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd 
        http://www.springframework.org/schema/context 
        https://www.springframework.org/schema/context/spring-context.xsd 
        http://www.springframework.org/schema/mvc 
        https://www.springframework.org/schema/mvc/spring-mvc.xsd">
    <!--开启注解扫描 : 此处属性 base-package 的范围与applicationContext.xml文件的范围一致 -->
    <context:component-scan base-package="com.msr">
        <!--只扫描那个注解-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>
    <!--配置视图解析器-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/pages/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
    <!--过滤静态资源-->
    <mvc:resources mapping="/css/**" location="/css/"/>
    <mvc:resources mapping="/images/**" location="/images/"/>
    <mvc:resources mapping="/js/**" location="/js/"/>
    <!--开启springmvc的注解扫描-->
    <mvc:annotation-driven></mvc:annotation-driven>
</beans>

按上述配置文件整合后,声明式事务失效,原因如下 : 

事务控制是spring提供的,而如果由springmvc容器扫描service层和dao层进行创建的数据库的操作对象,就不再支持事务控制。

上面的配置中spring的两次扫描包的范围一致,服务器启动时先加载applicationContext.xml文件(简称父容器),再加载springmvc.xml文件(简称子容器),那么子容器中的service对象会覆盖父容器中的service对象.而声明式事务管理是在父容器中声明才会起作用,被子容器覆盖后失效.

解决方案有两种,如下 : 

1.修改springmvc.xml中 context:component-scan 的 base-package的属性值详细到包名,如下:

    <!-- 此处属性 base-package 的范围详细到控制层的包名 -->
    <context:component-scan base-package="com.msr.controller">
        <!--只扫描那个注解-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
    </context:component-scan>

2.不修改springmvc.xml中的base-package的属性值,而是添加不扫描Service注解,如下 : 

   <context:component-scan base-package="com.msr">
        <!--只扫描那个注解-->
        <context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
        <!--不扫描Service注解-->
        <context:exclude-filter type="annotation" expression="org.springframework.stereotype.Service"/>
    </context:component-scan>

推荐使用方式一.

另可参考 : https://www.cnblogs.com/zhaojz/p/8176052.html 文档的详解

 

posted @ 2021-01-28 16:55  -超级菜鸟-  阅读(203)  评论(0编辑  收藏  举报