AOP应用-用aspectJ解决事务管理

1、

package com.service;

import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.dao.IAccountDao;
import com.dao.IStockDao;
import com.exception.BuyStockException;

public class BuyStockServiceImpl implements IBuyStockService {
	private IAccountDao adao;
	private IStockDao sdao;
	
	public void setAdao(IAccountDao adao) {
		this.adao = adao;
	}

	public void setSdao(IStockDao sdao) {
		this.sdao = sdao;
	}

	@Override
	public void openAccount(String aname, double money) {
		adao.insertAccount(aname, money);
	}

	@Override
	public void openStock(String sname, int amount) {
		sdao.insertStock(sname, amount);
	}

	@Override
	public void buyStock(String aname, double money, String sname, int amount) throws BuyStockException{
		boolean isBuy = true;
		adao.updateAccount(aname, money, isBuy);
		if (true) {
			throw new BuyStockException("购买股票异常");
		}
		sdao.updateStock(sname, amount, isBuy);
	}

}

2、

<?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/tx 
        http://www.springframework.org/schema/tx/spring-tx.xsd
        http://www.springframework.org/schema/aop 
        http://www.springframework.org/schema/aop/spring-aop.xsd">
	
	<!--=====================================IOC============================================-->
	
	<!-- 注册dataSource:myDataSource -->
	<bean id="myDataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
		<property name="driverClass" value="${jdbc.driver}"/>
		<property name="jdbcUrl" value="${jdbc.url}"/>
		<property name="user" value="${jdbc.user}"/>
		<property name="password" value="${jdbc.password}"/>
	</bean>
	<!-- 加载jdbc.properties -->
	<context:property-placeholder location="classpath:jdbc.properties"/>
	
	<!-- 注册dao:myAccountDao -->
	<bean id="myAccountDao" class="com.dao.AccountDaoImpl">
		<property name="dataSource" ref="myDataSource"/>
	</bean>
	
	<!-- 注册dao:myStockDao -->
	<bean id="myStockDao" class="com.dao.StockDaoImpl">
		<property name="dataSource" ref="myDataSource"/>
	</bean>
	
	<!-- 注册service:myBuyStockService -->
	<bean id="myBuyStockService" class="com.service.BuyStockServiceImpl">
		<property name="adao" ref="myAccountDao"/>
		<property name="sdao" ref="myStockDao"/>
	</bean>
	
	<!--=====================================AOP============================================-->
	<!-- 注册事务管理器 -->
	<bean id="myTransactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
		<property name="dataSource" ref="myDataSource"/>
	</bean>
	
	<!-- 注册事务通知 -->
	<tx:advice id="txAdvice" transaction-manager="myTransactionManager">
		<tx:attributes>
			<!--这里指定的是:为每一个连接点指定所要应用的事务属性  -->
			<tx:method name="open*" isolation="DEFAULT" propagation="REQUIRED"/>
			<tx:method name="buyStock" isolation="DEFAULT" propagation="REQUIRED" rollback-for="BuyStockException"/>
		</tx:attributes>
	</tx:advice>
	
	<!-- AOP配置 -->	
	<aop:config>
		<!--这里指定的是切入点  -->
		<aop:pointcut expression="execution(* *..IBuyStockService.buyStock(..))" id="myPointcut"/>
		<aop:advisor advice-ref="txAdvice" pointcut-ref="myPointcut"/>
	</aop:config>
	
</beans>
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值