基于XML配置声明式事务

基于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:tx="http://www.springframework.org/schema/tx"
       xmlns:aop="http://www.springframework.org/schema/aop"
       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/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/aop https://www.springframework.org/schema/aop/spring-aop.xsd">

    <!--    扫描包-->
    <context:component-scan base-package="com.lwt"></context:component-scan>
    <context:property-placeholder location="classpath:dbconfig.properties"></context:property-placeholder>
<!--    配置德鲁伊数据源-->
    <bean id="dataSource" class="com.alibaba.druid.pool.DruidDataSource">
        <property name="username" value="${usernamelwt}"></property>
        <property name="password" value="${password}"></property>
        <property name="url" value="${url}"></property>
        <property name="driverClassName" value="${driverClassName}"></property>
    </bean>
<!--    配置JdbcTemplate-->
    <bean class="org.springframework.jdbc.core.JdbcTemplate">
<!--        <property name="dataSource" ref="dataSource"></property>-->
        <property name="dataSource" value="#{dataSource}"></property>
    </bean>
<!--    事务控制
        1.配置事务管理器,导入1个基础版+3个增强版的切面jar包
-->
    <bean id="dataSourceTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>


<!--&lt;!&ndash;    2.开启基于注解的事务控制模式,依赖tx名称空间&ndash;&gt;-->
<!--    <tx:annotation-driven transaction-manager="dataSourceTransactionManager"></tx:annotation-driven>-->
<!--&lt;!&ndash;    3.给事务方法加注解&ndash;&gt;-->

<!--    基于xml配置的事务,依赖tx、aop名称空间
       1.Spring提供事务管理器(切面),配置事务管理器bean
       2.配置出事务方法
       3.哪些方式是事务方法,事务切面按照切入点表达式切入事务方法
-->
    <aop:config>
        <aop:pointcut id="txPointCut" expression="execution( * com.lwt.xml_tx.*.*(..))"/>
<!--        advice-ref=""指向事务管理器的配置-->
        <aop:advisor advice-ref="myAdvice" pointcut-ref="txPointCut"></aop:advisor>
    </aop:config>
<!--    配置事务管理器
    transaction-manager指定配置哪个事务管理器,相当于注解@transacional
    tx:advice 事务建议|事务增强|事务属性
-->
    <tx:advice id="myAdvice" transaction-manager="dataSourceTransactionManager">
<!--        事务属性tx:attributes-->
        <tx:attributes>
<!--            指明哪些方法是事务方法tx:method,和切入点表达式的方法对比,哪些方法加事务用tx:method指定
                *给所有方法都加事务
-->
            <tx:method name="*"/>
            <tx:method name="checkout" propagation="REQUIRED" timeout="-1"></tx:method>
            <tx:method name="get*" read-only="true"></tx:method>
        </tx:attributes>
    </tx:advice>
</beans>
public class TxTest {
    ApplicationContext ioc = new ClassPathXmlApplicationContext("spring-tx_xml.xml");
    @Test
    public void test1() throws FileNotFoundException {
        TxBookService bookService = ioc.getBean(TxBookService.class);
        bookService.checkout("Tom","ISBN-001");
        System.out.println("结账成功");
//
//        int price = bookService.getPrice("ISBN-001");
//        System.out.println("读取修改了未提交的数据"+price);
//        price=price-51;
//        System.out.println("进行一系列运用后价格为:"+price);
//
//        // 有事务的业务逻辑,容器中保存的事这个业务逻辑的代理对象
//        System.out.println(bookService.getClass());// 无事务class com.lwt.tx.TxBookService。有事务class com.lwt.tx.TxBookService$$EnhancerBySpringCGLIB$$2cb3d5bb

    }

    /**
     * 多事务
     */
    @Test
    public void test2() throws FileNotFoundException {
        MultService multService = ioc.getBean(MultService.class);
        multService.mulTx();
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值