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>