spring(AOP事务)

事务操作

  1. 打开事务
  2. 提交事务
  3. 回滚事务 

 事务操作对象

 不同平台操作事务的代码不相同,spring提供了一个接口

 PlatformTransactionManager 接口
    DataSourceTransactionManager
    HibernateTransitionmanager
    注意:在spring中,事务管理.最为核心的对象就是TransactionManager对象 

 例子:银行转账

AccountServiceImpl.java 实现AccountService接口

 

public class AccountServiceImpl implements AccountService{
	private AccountDao ad ;
	
	@Override
	public void transfer(Integer from, Integer to, Double money) {
		ad.addMoney(from,money);
                //错误
		int a = 1/0;
		ad.loseMoney(to,money);
	}

	
	public void setAd(AccountDao ad) {
		this.ad = ad;
	}

}

AccountDaoImpl.java 继承JdbcDaoSupport 实现AccountDao接口

public class AccountDaoImpl extends JdbcDaoSupport implements AccountDao{

	@Override
	public void addMoney(Integer id, Double money) {
		String sql = "update t_account set money = money+? where id=?";
		getJdbcTemplate().update(sql, money,id);
	}

	@Override
	public void loseMoney(Integer id, Double money) {
		String sql = "update t_account set money = money-? where id=?";
		getJdbcTemplate().update(sql, money,id);
	}

}

db.properties

jdbc.jdbcUrl=jdbc:mysql:///web_spring
jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.user=root
jdbc.password=admin

 

applicationContext.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop" xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd ">
	<!-- 读取配置文件 -->
	<context:property-placeholder location="classpath:db.properties"/>
	
	<!-- 事务核心管理器,封装了所有事务操作. 依赖于连接池 -->
	<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" >
		<!-- 注入连接池 -->
		<property name="dataSource" ref="dataSource" ></property>
	</bean>
	<!-- 配置事务通知 -->
	<tx:advice id="txAdvice" transaction-manager="transactionManager">
		<tx:attributes>
			<!-- transfer方法策略 -->
			<tx:method name="transfer" isolation="REPEATABLE_READ" propagation="REQUIRED" read-only="false"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- 配置织入 -->
	<aop:config>
		<!-- 配置切点表达式 -->
		<aop:pointcut expression="execution(* spring.tx.*ServiceImpl.*(..))" id="tx"/>
		
		<!-- 配置切面 : 通知+切点
		 	advice-ref:通知的名称
		 	pointcut-ref:切点的名称
	 	-->
		<aop:advisor advice-ref="txAdvice" pointcut-ref="tx"/>
	</aop:config>
	
	<!-- 连接池 -->
	<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
		<property name="driverClass" value="${jdbc.driverClass}"></property>
		<property name="user" value="${jdbc.user}"></property>
		<property name="password" value="${jdbc.password}"></property>
	</bean>
	
	<!-- Dao -->
	<bean name="accountDao" class="spring.tx.AccountDaoImpl">
		<property name="dataSource" ref="dataSource"></property>
	</bean>
	<!-- Service -->
	<bean name="accountService" class="spring.tx.AccountServiceImpl">
		<property name="ad" ref="accountDao"></property>
	</bean>
</beans>

测试:


@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext1.xml")
public class A_Hello {
	
	@Resource(name="accountService")
	private AccountService as;
	
	@Test
	public void func1(){
		as.transfer(1, 2, 100d);
	}
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值