Spring_事务(基于xml文件的方式)

applicationContext-tx-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 http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd">

    <!-- 导入资源文件 -->
    <context:property-placeholder location="classpath:db.properties" />

    <!--配置c3p0数据源 -->
    <bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
        <property name="user" value="${jdbc.user}"></property>
        <property name="password" value="${jdbc.password}"></property>
        <property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
        <property name="driverClass" value="${jdbc.driverClass}"></property>

        <property name="initialPoolSize" value="${jdbc.initPoolSize}"></property>
        <property name="maxPoolSize" value="${jdbc.maxPoolSize}"></property>
    </bean>

    <!--配置Spring 的JdbcTemplate -->
    <bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
        <property name="dataSource" ref="dataSource"></property>
    </bean>


    <!-- 配置bean -->
    <bean id="bookShopDao" class="com.aff.spring.tx.xml.BookShopDaoImpl">
        <property name="jdbcTemplate" ref="jdbcTemplate"></property>
    </bean>

    <bean id="bookShopService" class="com.aff.spring.tx.xml.service.impl.BookShopServiceImpl">
        <property name="bookShopDao" ref="bookShopDao"></property>
    </bean>

    <bean id="cashier" class="com.aff.spring.tx.xml.service.impl.CashierImpl">
        <property name="bookShopService" ref="bookShopService"></property>
    </bean>


    <!--1. 配置事务管理器 -->
    <bean id="transactionManager"
        class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
        <property name="dataSource" ref="dataSource"></property>
    </bean>
    <!-- 2.配置事务属性 -->
    <tx:advice id="txAdvice" transaction-manager="transactionManager">
        <tx:attributes>
            <!-- 根据方法名指定事务的属性 -->
            <tx:method name="purchase" propagation="REQUIRES_NEW" />
            <tx:method name="get*" read-only="true" />
            <tx:method name="find*" read-only="true" />
            <tx:method name="" />
            <tx:method name="*" />
        </tx:attributes>
    </tx:advice>
    <!--3.配置事务切入点,以及把事务切入点和事务属性关联起来 -->
    <aop:config>
        <!--所有接口的所有方法 -->
        <aop:pointcut expression="execution( * com.aff.spring.tx.xml.service.*.*(..))"
            id="txPointCut" />
        <!-- 4.把事务切面和属性个关联起来 -->
        <aop:advisor advice-ref="txAdvice" pointcut-ref="txPointCut" />
    </aop:config>

</beans>

 

BookShopDaoImpl.java

package com.aff.spring.tx.xml;

import org.springframework.jdbc.core.JdbcTemplate;

public class BookShopDaoImpl implements BookShopDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
        this.jdbcTemplate = jdbcTemplate;
    }

    @Override
    public int findBookPriceByIsBn(String isbn) {
        String sql = "select price  from book where isbn = ?";
        return jdbcTemplate.queryForObject(sql, Integer.class, isbn);

    }

    @Override
    public void updateBookStock(String isbn) {
        // 检查书的库存是否足够, 若不够抛出异常
        String sql2 = "select stock from book_stock where isbn = ?";
        int stock = jdbcTemplate.queryForObject(sql2, Integer.class, isbn);
        if (stock == 0) {
            throw new BookStockException("库存不足");
        }

        String sql = "update book_stock set stock = stock -1 where isbn = ?";
        jdbcTemplate.update(sql, isbn);

    }

    @Override
    public void updateUserAcount(String username, int price) {
        // 验证余额是否足够, 若不足则抛出异常
        String sql2 = "select balance from account where username = ?";
        int balance = jdbcTemplate.queryForObject(sql2, Integer.class, username);
        if (balance < price) {
            throw new UserAccountException("余额不足");
        }

        String sql = "update account set balance  = balance-? where  username = ?";
        jdbcTemplate.update(sql, price, username);

    }
}

 

 BookShopServiceImpl.java

package com.aff.spring.tx.xml.service.impl;

import com.aff.spring.tx.xml.BookShopDao;
import com.aff.spring.tx.xml.service.BookShopService;

public class BookShopServiceImpl implements BookShopService {
    
    private BookShopDao bookShopDao;
    public void setBookShopDao(BookShopDao bookShopDao) {
        this.bookShopDao = bookShopDao;
    }

    public void purchase(String username, String isbn) {
        // 1.获取书的单价
        int price = bookShopDao.findBookPriceByIsBn(isbn);

        // 2.跟新书的库存
        bookShopDao.updateBookStock(isbn);

        // 3. 跟新用户余额
        bookShopDao.updateUserAcount(username, price);
    }

}

 

CashierImpl.java

package com.aff.spring.tx.xml.service.impl;

import java.util.List;

import com.aff.spring.tx.xml.service.BookShopService;
import com.aff.spring.tx.xml.service.Cashier;

public class CashierImpl implements Cashier {
    private BookShopService bookShopService;
    public void setBookShopService(BookShopService bookShopService) {
        this.bookShopService = bookShopService;
    }

    @Override
    public void checkout(String username, List<String> isbns) {
        for (String isbn : isbns) {
            bookShopService.purchase(username, isbn);

        }

    }

}

 

SpringTransactionTest.java

package com.aff.spring.tx.xml;
import java.util.Arrays;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.aff.spring.tx.xml.service.BookShopService;
import com.aff.spring.tx.xml.service.Cashier;

public class SpringTransactionTest {
    private ApplicationContext ctx = null;
    private BookShopDao bookShopDao = null;
    private BookShopService bookShopService = null;
    private Cashier cashier = null;

    {
        ctx = new ClassPathXmlApplicationContext("applicationContext-tx-xml.xml");
        bookShopDao = ctx.getBean(BookShopDao.class);
        bookShopService = ctx.getBean(BookShopService.class);
        cashier = ctx.getBean(Cashier.class);
    }

    // 事务的传播
    @Test
    public void testTransactionlPropagetion() {
        cashier.checkout("AA", Arrays.asList("1001","1002"));
    }

    @Test
    public void testBookShopService() {
        bookShopService.purchase("AA", "1001");
    }
}

 

目录

 

posted @ 2020-05-30 14:59  林淼零  阅读(207)  评论(0编辑  收藏  举报