Spring入门之AOP事物的实现
一、原理
因为在业务层中我们会有很多业务方法,比如银行转账业务(从目标:from中扣钱,向目的地:target增加)都需要事物的介入,然而Spring的AOP技术出现后,我们可以很容易的使用环绕通知来解决这一难题(很多业务都需要事物管理,开发者手动添加事物很累!)。
二、实现(xml)
这里我用模拟银行转账的小程序来实现事物管理
package cn.zw.tx;
/**
*
* @ClassName: BankDao
* @Description:TODO(模拟银行转账接口)
* @date: 2018年12月3日 下午8:21:26
*/
public interface BankDao {
/**
*
* @Title: BankDao
* @Description:转入
* @param: @param target 目标账户
* @param: @param money 金额
* @throws
*/
void increaseMoney(int target , double money);
/**
*
* @Title: BankDao
* @Description:转出
* @param: @param target 目标账户
* @param: @param money 金额
* @throws
*/
void decreaseMoney(int target ,double money);
}
package cn.zw.tx;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
public class BankDaoImpl extends JdbcDaoSupport implements BankDao {
/**
*
* <p>Title: increaseMoney</p>
* <p>Description:转入 </p>
* @param target 目标账户
* @param money 转入金额
* @see cn.zw.tx.BankDao#increaseMoney(int, double)
*/
@Override
public void increaseMoney(int target, double money) {
String sql = "update user set money = money + ? where id = ?";
getJdbcTemplate().update(sql,money,target);
}
/**
*
* <p>Title: decreaseMoney</p>
* <p>Description:转出 </p>
* @param target 目标账户
* @param money 转出金额
* @see cn.zw.tx.BankDao#decreaseMoney(int, double)
*/
@Override
public void decreaseMoney(int target, double money) {
String sql = "update user set money = money - ? where id = ?";
getJdbcTemplate().update(sql,money,target);
}
}
package cn.zw.tx;
/**
*
* @ClassName: BankService
* @Description: 转账的业务层
*/
public interface BankService {
/**
*
* @Title: BankService
* @Description: 转账业务
* @param: @param from 从哪个账务转出
* @param: @param to 向哪个账户转入
* @param: @param money 转入金额
* @throws
*/
void transfer(int from ,int to,double money);
}
package cn.zw.tx;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class BankServiceImpl implements BankService {
@Autowired
private BankDao db;
/**
*
* <p>Title: transfer</p>
* <p>Description: 转账业务 </p>
* @param: from 从哪个账务转出
* @param: to 向哪个账户转入
* @param: money 转入金额
* @see cn.zw.tx.BankService#transfer(int, int, double)
*/
@Override
public void transfer(int from, int to, double money) {
db.decreaseMoney(from, money);
db.increaseMoney(to, money);
}
}
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd">
<context:property-placeholder location="classpath:c3p0.properties" />
<context:component-scan base-package="cn.zw.tx"></context:component-scan>
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean name="bankDaoImpl" class="cn.zw.tx.BankDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
</beans>
`
package cn.zw.tx;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration("classpath:application-context8.xml")
public class DemoTest {
@Autowired
private BankService bs;
@Test
public void fun01(){
bs.transfer(1, 2, 100D);
}
}
向代码中加入了异常,模拟停电
执行结果
将代码进行改造
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<context:property-placeholder location="classpath:c3p0.properties" />
<context:component-scan base-package="cn.zw.tx"></context:component-scan>
<!-- 配置数据库连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean name="bankDaoImpl" class="cn.zw.tx.BankDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 数据库实务管理类 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 定义通知 -->
<tx:advice id="txAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="transfer" read-only="false" timeout="-1" propagation="REQUIRED" isolation="DEFAULT"/>
</tx:attributes>
</tx:advice>
<!-- 织入 -->
<aop:config>
<aop:pointcut expression="execution(* cn.zw.tx.*ServiceImpl.*(..))" id="myPC"/>
<aop:advisor advice-ref="txAdvice" pointcut-ref="myPC"/>
</aop:config>
</beans>
搞定!
三、实现(注解)
`
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://www.springframework.org/schema/beans"
xmlns:p="http://www.springframework.org/schema/p"
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-4.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.2.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.2.xsd">
<context:property-placeholder location="classpath:c3p0.properties" />
<context:component-scan base-package="cn.zw.tx"></context:component-scan>
<!-- 配置数据库连接池 -->
<bean name="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${jdbc.user}"></property>
<property name="driverClass" value="${jdbc.driverClass}"></property>
<property name="jdbcUrl" value="${jdbc.jdbcUrl}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<bean name="bankDaoImpl" class="cn.zw.tx.BankDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 数据库实务管理类 -->
<bean name="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 开启事物注解 -->
<tx:annotation-driven transaction-manager="transactionManager"/>
</beans>