以银行转账为例(本文只作为自己学习spring的备忘录,只对自己负责)
转账的dao层接口
转账dao层接口实现类
业务逻辑接口实现类
- package com.slowly.springtrancation01;
- import org.springframework.transaction.TransactionStatus;
- import org.springframework.transaction.support.TransactionCallbackWithoutResult;
- import org.springframework.transaction.support.TransactionTemplate;
- public class accountServiceImp implements accountService {
- private accountDao accountDao;
- public void setAccountDao(accountDao accountDao) {
- this.accountDao = accountDao;
- }
-
- @Override
- public void transfer(String out, String in, Double money) {
- accountDao.outMoney(out, money);
- //int i = 1/0;
- accountDao.inMoney(in, money);
- }
- }
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:aop="http://www.springframework.org/schema/aop"
xmlns:context="http://www.springframework.org/schema/context"
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">
<!-- 引入外部的文件 -->
<context:property-placeholder location="classpath:jdbc.properties"/>
<!-- 配置c3p0的连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="${driverClass}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
</bean>
<!-- 配置业务层 -->
<bean id="accountService" class="com.slowly.springtrancation02.accountServiceImp">
<property name="accountDao" ref="accountDao"></property>
</bean>
<!-- 配置dao层 -->
<bean id="accountDao" class="com.slowly.springtrancation02.accountDaoImp">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 事务管理配置 -->
<bean id="tranctionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置业务层的代理 -->
<bean id="accountServiceProxy" class="org.springframework.transaction.interceptor.TransactionProxyFactoryBean">
<!-- 配置目标对象 -->
<property name="target" ref="accountService"/>
<!-- 配置事务管理器 -->
<property name="transactionManager" ref="tranctionManager"/>
<!-- <property name="proxyTargetClass" value="true"></property> -->
<!-- 注入事务属性 -->
<property name="transactionAttributes">
<props>
<prop key="transfer">PROPAGATION_REQUIRED</prop>
</props>
</property>
</bean>
<!-- <bean id="myProxy" parent="accountServiceProxy">
<property name="target" ref="accountService"/>
</bean> -->
</beans>
package com.slowly.springtrancation02;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
public class Test {
public static void main(String[] args) {
ApplicationContext apt = new ClassPathXmlApplicationContext("applicationContext2.xml");
accountService accountService = (com.slowly.springtrancation02.accountService) apt.getBean("accountServiceProxy");
accountService.transfer("乙", "甲", 600.00);
}
}
测试用的和上文一样的数据库表