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. python学习之输出与文件读写

    #1. 打印字符串print ("His name is %s"%("Aviad")) #2.打印整数print ("He is %d years o ...

  2. HDU - 5936: Difference(暴力:中途相遇法)

    Little Ruins is playing a number game, first he chooses two positive integers yy and KK and calculat ...

  3. yield 与生成器

    yield的功能类似于return,但是不同之处在于它返回的是生成器. 生成器 生成器是通过一个或多个yield表达式构成的函数,每一个生成器都是一个迭代器(但是迭代器不一定是生成器). 如果一个函数 ...

  4. 剑指offer-第五章优化时间和空间效率(连续子数组的最大和)

    题目:输入一个数组,数组中有正也有负,数组中连续的一个或者连续的多个数字组成一个子数组.求所有的子数组和的最大值.要求时间复杂度为O(n) 思路:我们的最直观的想法就是求出这个数组中的所有的子数组,然 ...

  5. python操作rabbitmq、redis

    1.启动rabbimq.mysql 在“”运行“”里输入services.msc,找到rabbimq.mysql启动即可 2.启动redis 管理员进入cmd,进入redis所在目录,执行redis- ...

  6. 一分钟理解js闭包

    什么是闭包?先看一段代码: ? 1 2 3 4 5 6 7 8 9 10 function a(){   var n = 0;   function inc() {     n++;     cons ...

  7. java代码求输入的三个数的最大值

    package com.c2; import java.util.Random; import java.util.Scanner; //输入10个数,找出最大一个数,并打印出来. public cl ...

  8. Annotation之一:Java Annotation基本功能介绍

    一.元数据的作用 如果要对于元数据的作用进行分类,目前还没有明确的定义,不过我们可以根据它所起的作用,大致可分为三类: 编写文档:通过代码里标识的元数据生成文档.这是最常见的,也是java 最早提供的 ...

  9. Idea 如何设置微软雅黑等其它字体

    使用过idea的同学都知道,idea的功能相对于explise来说,功能太强大了啊~个人感觉,idea真心挺智能的.但是,这里有一个小瑕疵,就是能够设置的字体,有限! 对于用惯了 微软雅黑 字体的人, ...

  10. python开发模块基础:re正则

    一,re模块的用法 #findall #直接返回一个列表 #正常的正则表达式 #但是只会把分组里的显示出来#search #返回一个对象 .group()#match #返回一个对象 .group() ...