Spring 声明式事务管理方式

时间:2021-10-25 06:26:02

声明式事务管理,基于AOP对目标代理,添加环绕通知,比编码方案优势,不具有侵入式,不需要修改原来的代码.

1.基于XML配置的声明式事务管理方案(案例)

     接口Service

public interface IAccountService {
public void account(String outname,String inname,double money);
}

service实现类

//@Transactional()注解时使用
public class AccountServiceImpl implements IAccountService{
@Autowired
private IAccountDao accountDao;

public void setAccountDao(IAccountDao accountDao) {
this.accountDao = accountDao;
}
//转账操作的方法
@Override
public void account(String outname, String inname, double money) {
//从outname转出
accountDao.accountOut(outname,money);
int a=10/0;
//从inname转入
accountDao.accountIn(inname,money);
//setter注入
}

Dao接口

public interface IAccountDao {

    public void accountOut(String outname, double money);

    public void accountIn(String inname, double money);

}

Dao实现类

 //import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
//由于JdbcDaoSupport的父类继承了Daosupport,它创建了JdbcTemplate,前提我们先注入一个dataSource
public class AccountDaoImpl extends JdbcDaoSupport implements IAccountDao{
//private JdbcTemplate jdbcTemplate;
//从outname中取钱
@Override
public void accountOut(String outname, double money) {
this.getJdbcTemplate().update("update account set money=money-? where name= ?",money,outname);
}
//向inname中存钱
@Override
public void accountIn(String inname, double money) {
this.getJdbcTemplate().update("update account set money=money+? where name= ?",money,inname);
} }

测试类

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath:applicationContext.xml")
public class accountServiceTest {
@Autowired
private IAccountService accountService;
@Test
public void test1() {
accountService.account("tom", "fox", 500);
}
}

applicationContext.xml配置文件

 <?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:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
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/aop http://www.springframework.org/schema/aop/spring-aop.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd"> <!-- 引入外部的properties文件 -->
<context:property-placeholder location="classpath:db.properties" />
<!-- 创建c3p0连接滨 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${jdbc.driverClass}" />
<property name="jdbcUrl" value="${jdbc.url}" />
<property name="user" value="${jdbc.username}" />
<property name="password" value="${jdbc.password}" />
</bean>
<!-- service -->
<bean id="accountService" class="cn.itcast.service.AccountServiceImpl">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- dao -->
<bean id="accountDao" class="cn.itcast.dao.AccountDaoImpl">
<!-- 当注入dataSource后,底层会自动创建一个JdbcTemplate Template入门案例 -->
<!-- c3p0和DriverManagerDatasource都可以创建datasource -->
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置事务管理器 -->
<bean id="transactionManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 配置通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<!--
name:必须的,对哪些方法进行事务控制
isolation 可选 设置事务隔离级别 默认是DEFAULT
propagation:可选 设置事务传播 默认值 REQUIRED
timeout 可选 超时时间 默认值-1
read-only 可选 默认值是false 如果不是只读,它可以对insert update delete操作,如果是只读不可以。
rollback-for 可选 可以设置一个异常,如果产生这个异常,触发事务回滚
no-rolback-for 可选 可以设置一个异常,如果产生这个异常,不会触发事务回滚
-->
<tx:method name="account" /> </tx:attributes>
</tx:advice> <!-- 配置切面 -->
<aop:config>
<aop:pointcut expression="execution(* cn.itcast.service.IAccountService.account(..))" id="txPointcut"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="txPointcut"/>
</aop:config>
<!-- 开启事务管理器 注解时使用-->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>

db.properties----------当我们需要频繁修改其中的内容,写到这个配置文件便于管理

jdbc.driverClass=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql:///springtest
jdbc.username=root
jdbc.password=123