二十一、Spring事务管理之基于AspectJ的注解方式

使用注解来配置事务管理就变得很简单了,继续在上一篇文章的环境上操作,没有看过上一篇文章的,可以先看二十、Spring事务管理之基于AspectJ的XML方式

修改一下配置文件,只需要添加下面这一行即可

<!-- 开启注解事务 -->
<tx:annotation-driven transaction-manager="transactionManager"/>

然后事务增强,AOP切面相关的都可以去掉了,完整的配置文件如下:

<?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">


	<!-- 注册c3p0连接池 -->
	<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<!-- c3p0的连接属性的名称有所不同,这里需要注意下 -->
		<property name="driverClass" value="com.mysql.jdbc.Driver" />
		<property name="jdbcUrl" value="jdbc:mysql:///spring_jdbc" />
		<property name="user" value="root" />
		<property name="password" value="1234" />

	</bean>

	<!-- 配置事务管理器 -->
	<bean id="transactionManager"
		class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<!-- 注入连接池,因为事务管理需要用到connection -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 开启注解事务 -->
	<tx:annotation-driven transaction-manager="transactionManager"/>

	
	<!-- 注册AccountDaoImpl -->
	<bean id="accountDao" class="blog.csdn.net.mchenys.dao.AccountDaoImpl">
		<!-- 注入属性 :dataSource属性来自JdbcDaoSupport类,只要继承就可以拥有 -->
		<property name="dataSource" ref="dataSource" />
	</bean>

	<!-- 注册AccountServiceImpl -->
	<bean id="accountService" class="blog.csdn.net.mchenys.service.AccountServiceImpl"/>
	
</beans>

然后,在业务层上添加一个注解:@Transactional即配置完成了.

package blog.csdn.net.mchenys.service;

import javax.annotation.Resource;

import org.springframework.transaction.annotation.Transactional;

import blog.csdn.net.mchenys.dao.AccountDao;
import blog.csdn.net.mchenys.domain.Account;

//service层实现类
@Transactional //添加这个注解在类上就可以对该类的所有dao操作进行事务管理了.
public class AccountServiceImpl implements AccountService {

	@Resource(name="accountDao") //通过注解注入dao
	private AccountDao accountDao;

	@Override
	public void pay(Account a, Account b, double money) {
		// A账户先扣钱
		accountDao.outMoney(a, money);

		// 模拟异常
		int num = 10 / 0;

		// B账户加钱
		accountDao.inMoney(b, money);
	}

	@Override
	public Account getAccountByName(String name) {
		return accountDao.getAccount(name);
	}

}


ok,这样就完成了。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值