Spring学习-34:Spring中的事务管理之声明式事务(基于注解)

本文介绍Spring框架中基于注解的声明式事务管理配置及应用示例,包括配置事务管理器、开启注解事务管理、在Service层使用@Transactional注解,并提供测试案例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Spring中的事务管理之声明式事务(基于注解)是基于<tx:annotation-driven/>,它默认查找名称为transactionmanager的事务管理器Bean。

这个很简单,请看例子。

首先,恢复项目环境。详见环境搭建。

1、配置applicationContxt.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/aop http://www.springframework.org/schema/aop/spring-aop.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"> 
<!-- 配置属性文件 -->  
<context:property-placeholder location="classpath:jdbc.properties"/>  
<!-- 配置C3P0连接池 -->  
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">  
    <property name="driverClass" value="${jdbc.driver}"></property>  
    <property name="jdbcUrl" value="${jdbc.url}"></property>  
    <property name="user" value="${jdbc.user}"></property>  
    <property name="password" value="${jdbc.password}"></property>  
</bean>  
<!-- 定义JDBC的模板类 -->  
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">  
    <property name="dataSource" ref="dataSource"></property>  
</bean>  
<!-- 业务层类 -->  
<bean id="accountService" class="com.js.demo4.AccountServiceImpl">  
    <property name="accountDao" ref="accountDao"></property>  
</bean>  
<!-- 持久层类 -->  
<bean id="accountDao" class="com.js.demo4.AccountDaoImpl">  
    <!-- 注入连接池对象,通过连接池对象去创建JDBC模板 -->  
    <property name="dataSource" ref="dataSource"></property>  
</bean> 
<!-- 事务管理器 -->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
	<property name="dataSource" ref="dataSource"/>
</bean>
<!-- 开启注解的事务管理 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>  
2、在Service上使用注解AccountServiceImpl:@Transactional


package com.js.demo4;

import org.springframework.transaction.annotation.Transactional;

@Transactional
public class AccountServiceImpl implements AccountService{
	private AccountDao accountDao;
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	/**
	 * 转账的方法
	 * @param from:从哪转出
	 * @param to:转入目标
	 * @param money:转账金额
	 */
	public void transfer(String from, String to,double money) {
				accountDao.out(from, money);
//				int i = 4/0;
				accountDao.in(to, money);
	}
}

该注解有很多属性,大家使用的话就会发现属性名并不陌生:


3、进行测试:

(3.1)有异常:

package com.js.demo4;

import org.springframework.transaction.annotation.Transactional;

@Transactional()
public class AccountServiceImpl implements AccountService{
	private AccountDao accountDao;
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	/**
	 * 转账的方法
	 * @param from:从哪转出
	 * @param to:转入目标
	 * @param money:转账金额
	 */
	public void transfer(String from, String to,double money) {
				accountDao.out(from, money);
				int i = 4/0;
				accountDao.in(to, money);
	}
}

package com.js.demo4;

import org.springframework.transaction.annotation.Transactional;

@Transactional()
public class AccountServiceImpl implements AccountService{
	private AccountDao accountDao;
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	/**
	 * 转账的方法
	 * @param from:从哪转出
	 * @param to:转入目标
	 * @param money:转账金额
	 */
	public void transfer(String from, String to,double money) {
				accountDao.out(from, money);
				int i = 4/0;
				accountDao.in(to, money);
	}
}


(3.2)无异常:

package com.js.demo4;

import org.springframework.transaction.annotation.Transactional;

@Transactional()
public class AccountServiceImpl implements AccountService{
	private AccountDao accountDao;
	public void setAccountDao(AccountDao accountDao) {
		this.accountDao = accountDao;
	}
	/**
	 * 转账的方法
	 * @param from:从哪转出
	 * @param to:转入目标
	 * @param money:转账金额
	 */
	public void transfer(String from, String to,double money) {
				accountDao.out(from, money);
//				int i = 4/0;
				accountDao.in(to, money);
	}
}

事务测试通过。使用起来很简单,不是吗?





评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值