spring中事务的aop实现

来源:互联网 发布:美国的消防员知乎 编辑:程序博客网 时间:2024/06/15 00:59

spring中事务的aop实现

在上一篇博客中我介绍了spring中aop的实现原理,同时也使用了service中的事务,但是都只是打印语句,并没有具体的实现,现在我把实现了的具体代码也提供出来。可以更好的理解aop
(1)、导包

(2)、创建表

(3)、写实体类以及service、dao
Account.java
public class Account {private Integer id;private String name;private Double money;public Integer getId() {return id;}public void setId(Integer 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;}}
AccountService.java
public interface AccountService {//转账void tranfer(int from,int to,double money);}
AccountServiceImpl.java
import org.springframework.beans.factory.annotation.Autowired;import org.springframework.transaction.annotation.Isolation;import org.springframework.transaction.annotation.Propagation;import org.springframework.transaction.annotation.Transactional;import com.haha.dao.AccountDao;public class AccountServiceImpl implements AccountService {@Autowiredprivate AccountDao accountDao;public void setAccountDao(AccountDao accountDao) {this.accountDao = accountDao;}@Overridepublic void tranfer(int from, int to, double money) {accountDao.fromMoney(from, money);//int i=1/0;accountDao.toMoney(to, money);System.out.println("转账成功");}}
AccountDao.java
public interface AccountDao {//减钱void fromMoney(int from,double money);//加钱void toMoney(int to,double money);}
AccountDaoImpl.java
import org.springframework.jdbc.core.support.JdbcDaoSupport;public class AccountdaoImpl extends JdbcDaoSupport implements AccountDao {//减钱@Overridepublic void fromMoney(int from, double money) {String sql="update account set  money=money-? where id=?";getJdbcTemplate().update(sql,money,from);}//加钱@Overridepublic void toMoney(int to, double money) {String sql="update account set  money=money+? where id=?";getJdbcTemplate().update(sql,money,to);}}
(4)、配置文件
applicationContext2.xml
<?xml version="1.0" encoding="UTF-8"?><beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns="http://www.springframework.org/schema/beans"xmlns:aop="http://www.springframework.org/schema/aop"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.xsdhttp://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsdhttp://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsdhttp://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd"><!--引入c3p0  --><context:property-placeholder location="classpath:db.properties"/><!--配置c3p0连接池  --><bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource"><property name="driverClass" value="${jdbc.driverClass}"/><property name="jdbcUrl" value="${jdbc.jdbcUrl}"/><property name="user" value="${jdbc.user}"/><property name="password" value="${jdbc.password}"/></bean><!--注入dao  --><bean name="accountDao" class="com.haha.dao.AccountdaoImpl"><property name="dataSource" ref="dataSource"/></bean><!--注入Service  --><bean name="accountService" class="com.haha.service.AccountServiceImpl"><property name="accountDao" ref="accountDao"/></bean><!--配置事务管理器  --><bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager" ><property name="dataSource" ref="dataSource"></property></bean><!-- 配置事务通知id:给通知起个名字transaction-manager:指定事务管理器是哪个 --><tx:advice id="myAdvice" transaction-manager="transactionManager"><tx:attributes><!--name:方法名,save*表示以save开头的任何方法isolation:隔离级别read-only:只读propagation:传播行为  --><tx:method name="save*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED"/><tx:method name="delect*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED"/><tx:method name="*" isolation="REPEATABLE_READ" read-only="false" propagation="REQUIRED"/></tx:attributes></tx:advice><!-- 配置织入 --><aop:config><!--配置切点  --><aop:pointcut expression="execution(* com.haha.service.*ServiceImpl.*(..))" id="txPC"/><!--配置切面 =通知+切点 --><aop:advisor advice-ref="myAdvice" pointcut-ref="txPC"/></aop:config></beans>
(5)、测试
import org.junit.Test;import org.junit.runner.RunWith;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.test.context.ContextConfiguration;import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;import com.haha.service.AccountService;@RunWith(SpringJUnit4ClassRunner.class)@ContextConfiguration("classpath:applicationContext2.xml")public class test2 {@Autowiredprivate AccountService accountService;public void setAccountService(AccountService accountService) {this.accountService = accountService;}@Testpublic void fun(){accountService.tranfer(1, 2, 100);}}
如果在转账的过程没有出现异常,它会正常执行,如果在转账的过程中出现了异常,导致转账只完成了一半,这个时候它会rollback,事务会回滚。