user=LF
password=LF
jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
driverClass=oracle.jdbc.driver.OracleDriver initialPoolSize=15
maxPoolSize=25
minPoolSize=5
<?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"> <!-- 引入外部文件 -->
<context:property-placeholder location="classpath:db.properties"/>
<!-- 配置dataSource -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="user" value="${user}"></property>
<property name="password" value="${password}"></property>
<property name="jdbcUrl" value="${jdbcUrl}"></property>
<property name="driverClass" value="${driverClass}"></property> <property name="initialPoolSize" value="${initialPoolSize}"></property>
<property name="maxPoolSize" value="${maxPoolSize}"></property>
<property name="minPoolSize" value="${minPoolSize}"></property>
</bean> <!-- 配置jdbcTemplate -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <bean id="amountDaoImpl" class="com.zr.trasaction.AmountDaoImpl.AmountDaoImpl">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <bean id="amountServiceImpl" class="com.zr.trasaction.AmountServiceImpl.AmountServiceImpl">
<property name="amountDaoImpl" ref="amountDaoImpl"></property>
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <!-- 配置事务管理器 -->
<bean id="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="transferMoneyService" propagation="REQUIRED"/>
<!-- read-only="true"只是为了数据库维护方便 -->
<tx:method name="transferMoneyService" read-only="true"/>
</tx:attributes>
</tx:advice>
<!-- 配置切面,让切面和事务关联起来 -->
<aop:config>
<aop:pointcut expression="execution(* com.zr.trasaction.AmountServiceImpl.*.*(..))" id="pointcut"/>
<aop:advisor advice-ref="txadvice" pointcut-ref="pointcut"/>
</aop:config> </beans>
package com.zr.trasaction.entity;

public class Amount {

    private String user;//用户名
private double money;//金额
public String getUser() {
return user;
}
public void setUser(String user) {
this.user = user;
}
public double getMoney() {
return money;
}
public void setMoney(double money) {
this.money = money;
}
public Amount() {
super();
}
public Amount(String user, double money) {
super();
this.user = user;
this.money = money;
}
@Override
public String toString() {
return "Amount [user=" + user + ", money=" + money + "]";
} }
package com.zr.trasaction.entity;

public class BalanceException extends RuntimeException {

    private static final long serialVersionUID = 1L;

    public BalanceException() {
super();
} public BalanceException(String message) {
super(message);
} public BalanceException(String message, Throwable cause) {
super(message, cause);
} public BalanceException(Throwable cause) {
super(cause);
} protected BalanceException(String message, Throwable cause,
boolean enableSuppression,
boolean writableStackTrace) {
super(message, cause, enableSuppression, writableStackTrace);
}
}
package com.zr.trasaction.AmountDao;

public interface AmountDao {

    /**
* 转出钱
* @return
*/
public boolean decreaseMoney(String username,double money) ; /**
* 转入钱
* @return
*/
public boolean increaseMoney(String username,double money); }
package com.zr.trasaction.AmountDaoImpl;

import org.springframework.jdbc.core.JdbcTemplate;

import com.zr.trasaction.AmountDao.AmountDao;

public class AmountDaoImpl implements AmountDao {

    private JdbcTemplate jdbcTemplate;

    public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} @Override
public boolean decreaseMoney(String username, double money) { String sql = "UPDATE TEST SET MONEY=MONEY-? WHERE USERNAME=?";
int i = -1;
i = jdbcTemplate.update(sql,money, username);
if(i>0){
return true;
}
return false;
} @Override
public boolean increaseMoney(String username, double money) {
String sql = "UPDATE TEST SET MONEY=MONEY+? WHERE USERNAME=?";
int i = -1;
i = jdbcTemplate.update(sql,money, username);
if(i>0){
return true;
}
return false;
} }
package com.zr.trasaction.AmountService;

import com.zr.trasaction.entity.Amount;

public interface AmountService {

    /**
* 转账业务
* @param desAmount 扣钱的账户
* @param incAmount 增加的账户
* @param money 转账的金额
* @return
*/
public boolean transferMoneyService(Amount desAmount,Amount incAmount,double money); }
package com.zr.trasaction.AmountServiceImpl;

import org.springframework.jdbc.core.JdbcTemplate;

import com.zr.trasaction.AmountDaoImpl.AmountDaoImpl;
import com.zr.trasaction.AmountService.AmountService;
import com.zr.trasaction.entity.Amount;
import com.zr.trasaction.entity.BalanceException; public class AmountServiceImpl implements AmountService { private AmountDaoImpl amountDaoImpl;
private JdbcTemplate jdbcTemplate; public void setAmountDaoImpl(AmountDaoImpl amountDaoImpl) {
this.amountDaoImpl = amountDaoImpl;
} public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
} @Override
public boolean transferMoneyService(Amount desAmount, Amount incAmount,
double money) {
String sql = "SELECT MONEY FROM TEST WHERE USERNAME=?";
double balance = jdbcTemplate.queryForObject(sql, Double.class, desAmount.getUser()); boolean success1 = amountDaoImpl.decreaseMoney(desAmount.getUser(), money);
boolean success2 = amountDaoImpl.increaseMoney(incAmount.getUser(), money); if(balance < money){
throw new BalanceException("余额不足");
}
return success1 && success2;
} }
package com.zr.trasaction.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext; import com.zr.trasaction.AmountService.AmountService;
import com.zr.trasaction.entity.Amount; public class TestA { public static void main(String[] args) { ApplicationContext ac = new ClassPathXmlApplicationContext("applicationContext.xml"); Amount desAmount = new Amount("lf", 0);
Amount incAmount = new Amount("tl", 0);
AmountService impl = (AmountService)ac.getBean("amountServiceImpl");
boolean isSuccess = impl.transferMoneyService(desAmount, incAmount, 10);
System.out.println("==:"+isSuccess);
} }

spring框架 事务 xml配置方式的更多相关文章

  1. spring框架 事务 注解配置方式

    user=LF password=LF jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl driverClass=oracle.jdbc.driver.Ora ...

  2. Spring MVC 的 XML 配置方式

    索引: 开源Spring解决方案--lm.solution 参看代码 GitHub: solution/pom.xml solution/webapi/pom.xml solution/mapper/ ...

  3. Spring 中使用XML配置方式和使用注解方式实现DI

    Spring容器给我们提供了很好的环境,我们只关注主要业务即可,其他的无需关注太多.今天刚学的DI DI(Dependency Injection):依赖注入 使用XML配置文件完成依赖注入 1.1普 ...

  4. 跟着刚哥学习Spring框架--通过XML方式配置Bean(三)

    Spring配置Bean有两种形式(XML和注解) 今天我们学习通过XML方式配置Bean 1. Bean的配置方式 通过全类名(反射)的方式   √ id:标识容器中的bean.id唯一. √ cl ...

  5. Spring框架(2)---IOC装配Bean(xml配置方式)

    IOC装配Bean (1)Spring框架Bean实例化的方式提供了三种方式实例化Bean 构造方法实例化(默认无参数,用的最多) 静态工厂实例化 实例工厂实例化 下面先写这三种方法的applicat ...

  6. 跟着刚哥学习Spring框架--事务配置(七)

    事务 事务用来保证数据的完整性和一致性. 事务应该具有4个属性:原子性.一致性.隔离性.持久性.这四个属性通常称为ACID特性.1.原子性(atomicity).一个事务是一个不可分割的工作单位,事务 ...

  7. web.xml中配置Spring中applicationContext.xml的方式

    2011-11-08 16:29 web.xml中配置Spring中applicationContext.xml的方式 使用web.xml方式加载Spring时,获取Spring applicatio ...

  8. Spring之AOP原理、代码、使用详解(XML配置方式)

    Spring 的两大核心,一是IOC,另一个是AOP,本博客从原理.AOP代码以及AOP使用三个方向来讲AOP.先给出一张AOP相关的结构图,可以放大查看. 一.Spring AOP 接口设计 1.P ...

  9. Spring框架事务支持模型的优势

    全局事务 全局事务支持对多个事务性资源的操作,通常是关系型数据库和消息队列.应用服务器通过JTA管理全局性事务,API非常烦琐.UserTransaction通常需要从JNDI获取,意味着需要与JND ...

随机推荐

  1. [转]MFC 调用 printf 输出

    摘自:http://blog.csdn.net/miyunhong/article/details/6704121 #include <io.h> #include <fcntl.h ...

  2. HDU - 6314:Matrix (广义容斥)(占位)

    Samwell Tarly is learning to draw a magical matrix to protect himself from the White Walkers. the ma ...

  3. Fillder手机抓包的使用

    1.Fillder下载地址: http://www.onlinedown.net/soft/73207.htm 2.网络设置 手机和电脑需链接网络相同 3.fillder设置 3.1打开fillder ...

  4. LA4992 Jungle Post

    题意 PDF 分析 炸连续的比炸单独的好. 二分答案,每种炸连续的构成一些半平面,判断半平面交是否为空. 时间复杂度\(O(T n \log^2)\) 代码 这题卡常,排序的时候必须事先算出幅角,不然 ...

  5. yarn 管理nextjs 项目

     预备环境 nodejs npm 1. yarn 安装 npm install -g yarn 2. nextjs 项目初始化 yarn add next react react-dom 3. 配置n ...

  6. serf  简单使用

    1. 介绍 // 以下为官方介绍,说白了就是进行系统的集群节点管理 Serf uses an efficient gossip protocol to solve three major proble ...

  7. Linux环境安装redis

    redis官网地址:http://www.redis.io/ 最新版本:2.8.3 在Linux下安装Redis非常简单,具体步骤如下(官网有说明): 1.下载源码,解压缩后编译源码. $ wget ...

  8. Android Socket编程

    花了大概两天的时间,终于把Android的Socket编程给整明白了.抽空和大家分享一下: Socket Programming on Android Socket 编程基础知识: 主要分服务器端编程 ...

  9. BufferedInputStream与BufferedOutputStream

    BufferedInputStream是带缓冲区的输入流,默认缓冲区大小是8M,能够减少访问磁盘的次数,提高文件读取性能:BufferedOutputStream是带缓冲区的输出流,能够提高文件的写入 ...

  10. zabbix 执行自定义key脚本超时

    报错如下: [root@master scripts]# /usr/local/zabbix/bin/zabbix_get -s 127.0.0.1 -k web.site.code[www.baid ...