Spring与Spring MVC事务配置及对比

首先先看看事务配置相关代码,这是公用的Spring直接配置在对应位置即可

事务相关配置application-mybatis.xml

<!-- 配置Spring的事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource" />
    </bean>

    <!-- 注解方式配置事物 -->
    <tx:annotation-driven transaction-manager="transactionManager" />
    <!-- 可以配置多个,针对不同的数据源 -->
    <!-- <tx:annotation-driven transaction-manager="transactionManager2" /> -->

    <!-- 拦截器方式配置事物 -->
    <tx:advice id="transactionAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 任何 RuntimeException 将触发事务回滚,但是任何 checked Exception 将不触发事务回滚 -->
            <tx:method name="*" 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="select*" propagation="SUPPORTS" />
        </tx:attributes>
    </tx:advice>
    <aop:config>
        <!-- 针对对应包下的所有方法 -->
        <!-- <aop:pointcut id="transactionPointcut" expression="execution(* com.aa.web..service.*.*(..))" /> -->
        <!-- com.aa下所有包和子包的有方法 -->
        <aop:pointcut id="transactionPointcut" expression="execution(* com.aa..*.*(..))" />
        <aop:advisor pointcut-ref="transactionPointcut"
            advice-ref="transactionAdvice" />
            <!-- 针对不同数据源或者不同的事务配置方式 -->
        <!-- <aop:advisor pointcut-ref="transactionPointcut"
            advice-ref="transactionAdvice2" /> -->
    </aop:config>

spring的话直接配置上面即可,但是mvc的话由于加载顺序问题,导致需要增加部分配置相关说明请看注释

web.xml

    <!-- 管理spring对象 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/applicationContext*.xml</param-value>
    </context-param>

    <listener>
        <description>SpringListener</description>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- MVC视图,管理请求 -->
    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring/spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
        <async-supported>true</async-supported>
    </servlet>
    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/mvc/spring-util-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        ">

    <!-- 管理spring对象 -->
    <!-- 加载所有配置文件 -->
    <context:property-placeholder location="classpath*:data/*.properties" />

    <!-- 自动扫描组件 -->
    <context:component-scan base-package="com.ys">
    <!-- 这里要把controler下面的 controller去除,他们是在spring-mvc.xml中配置的,不去除会影响事务管理 -->
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
    </context:component-scan>
</beans>   

spring-mvc.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:p="http://www.springframework.org/schema/p"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:tx="http://www.springframework.org/schema/tx"
    xmlns:util="http://www.springframework.org/schema/util" xmlns:aop="http://www.springframework.org/schema/aop"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd
        http://www.springframework.org/schema/util
        http://www.springframework.org/schema/mvc/spring-util-4.0.xsd
        http://www.springframework.org/schema/tx
        http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        ">

    <!-- MVC视图,管理请求 -->
    <!-- 启动Spring MVC的注解功能完成请求和注解POJO的映射 -->
    <context:component-scan base-package="com.ys">
        <context:include-filter type="annotation"
            expression="org.springframework.stereotype.Controller" />
        <!-- 排除了扫描@service的bean。 -->
        <!-- 由于web启动时,首先加载这个spring-servlet.xml文件,没有加载application等文件 -->
        <!-- 这时将生成一个没有事务的service实例注入到controller -->
        <context:exclude-filter type="annotation"
            expression="org.springframework.stereotype.Service" />
    </context:component-scan>

    <!-- should be last active -->
    <mvc:annotation-driven />

    <!-- 静态资源访问 /image/**的资源都从/image/里面进行查找。 -->
    <mvc:resources location="/images/" mapping="/images/**" />
    <mvc:resources location="/js/" mapping="/js/**" />
    <mvc:resources location="/css/" mapping="/css/**" />
</beans>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值