前言
這回來說下註解方式的聲明式事務管理。
正文
Demo
1、引入相關的jar包
這裏寫圖片描述
2、引入AOP約束
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://www.springframework.org/schema/beans"
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 ">
</beans>
3、準備目標對象
public interface AccountService {
//轉賬方法
void transfer(Integer from,Integer to,Double money);
}
/****/
public class AccountServiceImpl implements AccountService {
private AccountDao ad;
public void setAd(AccountDao ad) {
this.ad = ad;
}
@Override
//轉賬方法
public void transfer( Integer from, Integer to, Double money){
//減錢
ad.decreaseMoney(from, money);
//異常代碼
int i = 1/0;
//加錢
ad.increaseMoney(to, money);
}
}
4、開啓註解管理事務