一、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: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-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/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<!-- 指定spring读取properties配置
--> <context:property-placeholder location="classpath:db.properties"/>
<!-- 三步完成spring中配置依赖关系 -->
<!-- 1.将连接池放入Spring容器 -->
<!-- <bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="jdbcUrl" value="jdbc:mysql:///day01"></property>
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
</bean> -->
<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>
<!-- 2.将JDBCTemplete放入Spring容器 -->
<!-- <bean name="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> -->
<!-- 3.将userdao放入spring容器 -->
<bean name="userDaoImpl" class="com.aitiman.dao.UserDaoImpl">
<!-- <property name="jt" ref="jdbcTemplate"></property>
--> <property name="dataSource" ref="dataSource"></property>
</bean>
<bean name="accountDaoImpl" class="com.aitiman.dao.AccountDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务核心管理器,封装了所有事务操作,依赖于连接池 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务模版对象 -->
<bean name="transactionTemplate" class="org.springframework.transaction.support.TransactionTemplate">
<property name="transactionManager" ref="transactionManager"></property>
</bean>
<!-- service -->
<bean name="accountServiceImpl" class="com.aitiman.service.AccountServiceImpl">
<property name="tt" ref="transactionTemplate"></property>
<property name="ad" ref="accountDaoImpl"></property>
</bean>
<!-- 配置事务通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transfer" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
<!-- 加*号可以通配方法 -->
<tx:method name="save*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
<tx:method name="delete*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
<tx:method name="find*" isolation="DEFAULT" propagation="REQUIRED" read-only="true" />
<tx:method name="get*" isolation="DEFAULT" propagation="REQUIRED" read-only="true" />
<tx:method name="remove*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
<tx:method name="add*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
<tx:method name="update*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
<tx:method name="modify*" isolation="DEFAULT" propagation="REQUIRED" read-only="false" />
</tx:attributes>
</tx:advice>
<!-- 配置织入 -->
<aop:config>
<aop:pointcut expression="execution(* com.aitiman.service.*ServiceImpl.*(..))" id="txPC"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPC"/>
</aop:config>
<!-- 开启使用注解管理AOP事务,就不用配置上面的事务管理和织入了-->
<tx:annotation-driven/>
</beans>
二、Dao层
package com.aitiman.service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import com.aitiman.dao.AccountDao;
public class AccountServiceImpl implements AccountService {
private AccountDao ad;
private TransactionTemplate tt;
//使用注解做事务管理,以下注解也可以加在类上,加在类上就是全局的
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void transfer(Integer from, Integer to, Double money) {
ad.inMoney(to, money);
//制造异常
//int i = 1/0;
ad.outMoney(from, money);
}
//不用注解做事务管理
/* public void transfer(Integer from, Integer to, Double money) {
ad.inMoney(to, money);
//制造异常
//int i = 1/0;
ad.outMoney(from, money);
}*/
//代码的方式进行事务管理
/* @Override
public void transfer(Integer from, Integer to, Double money) {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
ad.inMoney(to, money);
ad.outMoney(from, money);
}
});
}*/
public void setAd(AccountDao ad) {
this.ad = ad;
}
public void setTt(TransactionTemplate tt) {
this.tt = tt;
}
}
三、service层
package com.aitiman.service;
import org.springframework.transaction.TransactionStatus;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.support.TransactionCallbackWithoutResult;
import org.springframework.transaction.support.TransactionTemplate;
import com.aitiman.dao.AccountDao;
public class AccountServiceImpl implements AccountService {
private AccountDao ad;
private TransactionTemplate tt;
//使用注解做事务管理,以下注解也可以加在类上,加在类上就是全局的
@Transactional(isolation=Isolation.REPEATABLE_READ,propagation=Propagation.REQUIRED,readOnly=false)
public void transfer(Integer from, Integer to, Double money) {
ad.inMoney(to, money);
//制造异常
//int i = 1/0;
ad.outMoney(from, money);
}
//不用注解做事务管理
/* public void transfer(Integer from, Integer to, Double money) {
ad.inMoney(to, money);
//制造异常
//int i = 1/0;
ad.outMoney(from, money);
}*/
//代码的方式进行事务管理
/* @Override
public void transfer(Integer from, Integer to, Double money) {
tt.execute(new TransactionCallbackWithoutResult() {
@Override
protected void doInTransactionWithoutResult(TransactionStatus arg0) {
ad.inMoney(to, money);
ad.outMoney(from, money);
}
});
}*/
public void setAd(AccountDao ad) {
this.ad = ad;
}
public void setTt(TransactionTemplate tt) {
this.tt = tt;
}
}
四、测试类
package com.aitiman.test;
import javax.annotation.Resource;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import com.aitiman.service.AccountService;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:applicationContext.xml")
public class Test03 {
@Resource(name="accountServiceImpl")
private AccountService as;
@Test
public void function() {
as.transfer(1, 2, 100d);
}
}
本文介绍Spring框架下如何通过XML配置及注解实现事务管理。主要内容包括:数据库连接池配置、JdbcTemplate配置、DAO层与Service层的事务处理方式,以及单元测试的实现。

648

被折叠的 条评论
为什么被折叠?



