Spring学习(十四)-声明式事务与传播行为

来源:互联网 发布:冰与火之歌世界观知乎 编辑:程序博客网 时间:2024/06/05 16:04

声明式事务:
1.配置事务管理器
2.启用事务注解
3.添加事务注解 @Transactional

1.BookShopServiceImpl.java实现类

package com.spring.tx;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Service("bookShopService")public class BookShopServiceImpl implements BookShopService {    @Autowired    public BookShopDao bookShopDao;    //添加事务注解    @Transactional    @Override    public void purchase(String username, String isbn) {        //1.获取书的单价        int price=bookShopDao.findBookPriceByIsbn(isbn);        //2.更新书的库存        bookShopDao.updateBookStock(isbn);        //3.更新用户余额        bookShopDao.updateUserAccount(username, price);    }}

2.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: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/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">  <context:component-scan base-package="com.spring"></context:component-scan>                       <!-- 导入资源文件 -->  <context:property-placeholder location="classpath:db.properties"/>  <!-- 配置C3p0数据源 -->  <bean id="dataSource"   class="com.mchange.v2.c3p0.ComboPooledDataSource" destroy-method="close">           <property name="driverClass" value="${jdbc.driverClass}"/>           <property name="jdbcUrl" value="${jdbc.jdbcUrl}"/>          <property name="user" value="${jdbc.user}"/>              <property name="password" value="${jdbc.password}"/>           <property name="initialPoolSize" value="${jdbc.initialPoolSize}"/>           <property name="minPoolSize" value="${jdbc.minPoolSize}"/>           <property name="maxPoolSize" value="${jdbc.maxPoolSize}"/>       </bean>  <!-- 配置Spring中JdbcTemplate(Jdbc模板类) --><bean id="jdbcTemplate"     class="org.springframework.jdbc.core.JdbcTemplate">    <property name="dataSource" ref="dataSource"/></bean><!-- 配置NameParameterJdbcTemplate该对象可以使用具名参数,其没有无参构造器,必须为其指定参数 --><bean id="nameParameterJdbcTemplate"    class="org.springframework.jdbc.core.namedparam.NamedParameterJdbcTemplate">    <constructor-arg ref="dataSource"></constructor-arg></bean><!-- 配置事务管理器 --><bean id="transactionManager"     class="org.springframework.jdbc.datasource.DataSourceTransactionManager">    <property name="dataSource" ref="dataSource"></property></bean><!-- 启用事务注解 --><tx:annotation-driven transaction-manager="transactionManager"/> </beans>

事务的传播行为:

1.Cashier.java接口

package com.spring.tx;import java.util.List;public interface Cashier {    public void checkout(String username,List<String> isbns);}

2.CashierImpl实现类

package com.spring.tx;import java.util.List;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.stereotype.Service;import org.springframework.transaction.annotation.Transactional;@Service("cashier")public class CashierImpl implements Cashier {    @Autowired    private BookShopService bookShopService;    @Transactional    @Override    public void checkout(String username, List<String> isbns) {        for(String isbn:isbns) {            bookShopService.purchase(username, isbn);        }    }}

测试方法

@Test    public void testTransactionlPropagation() {        cashier.checkout("AA", Arrays.asList("1001","1002"));    }
原创粉丝点击