spring学习笔记06:AOP事务的配置

一、导包(下面用一个转账的例子来实现):

<properties>
  		<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
  		<junit.version>4.12</junit.version>
  		<c3p0.version>0.9.5.2</c3p0.version>
  		<mysql.version>5.1.38</mysql.version>
  		<spring.version>5.0.8.RELEASE</spring.version>
  		<aspect.version>1.8.10</aspect.version>
  </properties>
  <dependencies>
  <!-- spring的aop支持 -->
  <dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjweaver</artifactId>
			<version>${aspect.version}</version>
		</dependency>
		<dependency>
			<groupId>org.aspectj</groupId>
			<artifactId>aspectjrt</artifactId>
			<version>${aspect.version}</version>
		</dependency>
  <!-- spring框架支持 -->
  <!-- spring上下文支持 -->
  <dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-context</artifactId>
			<version>${spring.version}</version>
		</dependency>
	<!-- spring-jdbc支持,使用JDBCTemplate -->
		<dependency>
		    <groupId>org.springframework</groupId>
		    <artifactId>spring-jdbc</artifactId>
		    <version>${spring.version}</version>
		</dependency>
	<!-- spring整合junit测试 -->
		<dependency>
			<groupId>org.springframework</groupId>
			<artifactId>spring-test</artifactId>
			<version>${spring.version}</version>
			<scope>test</scope>
		</dependency>
		<!-- mysql connector-->
	<dependency>
	    <groupId>mysql</groupId>
	    <artifactId>mysql-connector-java</artifactId>
	    <version>${mysql.version}</version>
	</dependency>
		<!-- c3p0支持 -->
		<dependency>
			<groupId>com.mchange</groupId>
			<artifactId>c3p0</artifactId>
			<version>${c3p0.version}</version>
		</dependency>
  <!-- junit测试支持 -->
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>${junit.version}</version>
      <scope>test</scope>
    </dependency>
  </dependencies>

二、创建Account实体类

public class Account {
	private int id;
	private String name;
	private double money;
	public int getId() {
		return id;
	}
	public void setId(int id) {
		this.id = id;
	}
	public String getName() {
		return name;
	}
	public void setName(String name) {
		this.name = name;
	}
	public double getMoney() {
		return money;
	}
	public void setMoney(double money) {
		this.money = money;
	}
	
}

三、创建对应的dao

接口
public interface AccountDao {
	//减钱操作
	public void reduce(int id,double money);
	//加钱操作
	public void increase(int id,double money);
}
实现类(我继承的JdbcDaoSupport,可以直接获取JdbcTemplate)
public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{
	//减钱操作
	@Override
	public void reduce(int id, double money) {
		String sql = "update t_account set money=money-? where id=?";
		this.getJdbcTemplate().update(sql, money,id);
	}
	//加钱操作
	@Override
	public void increase(int id, double money) {
		String sql = "update t_account set money=money+? where id=?";
		this.getJdbcTemplate().update(sql, money,id);
	}

}

四、创建对应的service

接口
public interface AccountService {
	//转账
	public void transfer(int id1, int id2, double money);
}
实现类
public class AccountServiceImpl implements AccountService{
	@Resource(name="accountDao")
	private AccountDao accountDao;
	
	
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}


	@Override
	public void transfer(int id1, int id2, double money) {
		//先减钱
		accountDao.reduce(id1,money);
		
		//增加异常
		//int a = 1/0;
		
		//再加钱
		accountDao.increase(id2, money);
	}

}

五、创建对一个数据库

数据库
插入两条测试数据:
在这里插入图片描述

六、配置文件

<?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.xsd
	http://www.springframework.org/schema/tx 
	http://www.springframework.org/schema/tx/spring-tx.xsd
	http://www.springframework.org/schema/aop
	http://www.springframework.org/schema/aop/spring-aop.xsd">
	<!-- 读取配置文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	<!-- dataSource -->
	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${dataSource.driverClassName}"/>
		<property name="jdbcUrl" 	 value="${dataSource.url}"/>
		<property name="user" 		 value="${dataSource.username}"/>
		<property name="password" 	 value="${dataSource.password}"/>
	</bean>
	<!-- dao注入dataSource -->
	<bean name="accountDao" class="com.spring_demo.transaction.dao.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<!-- service注入dao -->
	<bean name="accountService" class="com.spring_demo.transaction.service.AccountServiceImpl">
		<property name="accountDao" ref="accountDao"/>
	</bean>
	<!-- 开启注解 -->
	<context:annotation-config/>
	<!-- aop事务 -->
	<!-- 1.配置事务核心管理器,不同平台不一样-->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="dataSource"/>
	</bean>
	<!-- 2.配置事务通知,即通知对象 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
		<!--  配置需要事务管理的方法 name是方法名 isolation是隔离级别 propagation是传播行为 
		read-only默认为true,表示只能查询数据库,当需要修改数据库时,需要设置为false-->
			<tx:method name="transfer" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
			<!-- 可以这样配置 ,表示以增删改查开头的方法都需要进行事务管理
				<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
				<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
				<tx:method name="insert*" isolation="DEFAULT" propagation="REQUIRED" read-only="false"/>
				<tx:method name="query*" isolation="DEFAULT" propagation="REQUIRED" read-only="true"/>
			-->
		</tx:attributes>
	</tx:advice>
	<!--3. 配置AOP -->
	<aop:config>
		<!-- 配置切点,注意切点表达式的配置 -->
		<aop:pointcut expression="execution(* com.spring_demo.*.service.*ServiceImpl.*(..))" id="pc"/>
		<!-- 配置切面 -->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="pc"/>
	</aop:config>
</beans>

isolation是隔离级别 propagation是传播行为 ;

aop的隔离级别和传播行为

七、测试

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext-transaction.xml")
public class TestTransaction {
	@Resource(name="accountService")
	private AccountService accountService;
	
	@Test
	public void test1() {
        //转账50
		accountService.transfer(1, 2, 50);
	}
}

八、测试结果

正常情况下,执行测试代码,查看数据库,转账成功
转账过程出现异常(往AccountServiceImpl的transfer方法加入int a=1/0;),没有增加事务管理的话会出现A给B转钱,A扣了钱,B 却没有收到钱;增加事务管理后出现异常数据库会回滚,所以不会发生丢钱的结果:

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值