user=LF
password=LF
jdbcUrl=jdbc:oracle:thin:@localhost:1521:orcl
driverClass=oracle.jdbc.driver.OracleDriver initialPoolSize=15
maxPoolSize=30
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: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:component-scan base-package="com.zr.trasaction"></context:component-scan> <!-- 引用外部文件 -->
<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="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!-- 配置事务声明方式 -->
<tx:annotation-driven transaction-manager="transactionManager"/> </beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <context-param>
<param-name>contextConfiguration</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <servlet>
<servlet-name>AServlet</servlet-name>
<servlet-class>com.zr.trasaction.servlet.AServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>AServlet</servlet-name>
<url-pattern>/AServlet</url-pattern>
</servlet-mapping> </web-app>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> <context-param>
<param-name>contextConfiguration</param-name>
<param-value>/WEB-INF/applicationContext.xml</param-value>
</context-param> <listener>
<listener-class>org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener> <servlet>
<servlet-name>AServlet</servlet-name>
<servlet-class>com.zr.trasaction.servlet.AServlet</servlet-class>
</servlet> <servlet-mapping>
<servlet-name>AServlet</servlet-name>
<url-pattern>/AServlet</url-pattern>
</servlet-mapping> </web-app>
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.dao;

public interface AmountDao {

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

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository; import com.zr.trasaction.dao.AmountDao; @Repository
public class AmountDaoImpl implements AmountDao {
@Autowired(required=true)
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.Service;

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.ServiceImpl;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional; import com.zr.trasaction.Service.AmountService;
import com.zr.trasaction.daoImpl.AmountDaoImpl;
import com.zr.trasaction.entity.Amount;
import com.zr.trasaction.entity.BalanceException; @Service("amountServiceImpl")
public class AmountServiceImpl implements AmountService {
@Autowired(required=true)
private AmountDaoImpl amountDaoImpl;
@Autowired(required=true)
private JdbcTemplate jdbcTemplate; public void setAmountDaoImpl(AmountDaoImpl amountDaoImpl) {
this.amountDaoImpl = amountDaoImpl;
} public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
/**
* REQUIRED(默认值):在有transaction状态下执行;如当前没有transaction,则创建新的transaction
* 只读事务属性readOnly=true: 表示这个事务只读取数据但不更新数据, 这样可以帮助数据库引擎优化事务
* 超时事务属性timeout=3: 事务在强制回滚之前可以保持多久. 这样可以防止长期运行的事务占用资源.单位为秒,一般不设置,使用默认的
* noRollbackForClassName={"BalanceException"} 发生BalanceException异常不回滚
*/
@Transactional(propagation=Propagation.REQUIRED,isolation=Isolation.READ_COMMITTED,readOnly=true,timeout=3/*,noRollbackForClassName={"BalanceException"}*/)
@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.servlet;

import java.io.IOException;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils; import com.zr.trasaction.Service.AmountService;
import com.zr.trasaction.entity.Amount; public class AServlet extends HttpServlet { private static final long serialVersionUID = 1L; public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { WebApplicationContext wac = WebApplicationContextUtils.getRequiredWebApplicationContext(this.getServletContext());
System.out.println(wac);
Amount desAmount = new Amount("lf", 0);
Amount incAmount = new Amount("tl", 0);
AmountService impl = (AmountService) wac.getBean("amountServiceImpl");
boolean isSuccess = impl.transferMoneyService(desAmount, incAmount, 10);
System.out.println(isSuccess);
} public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException { } }

web 应用中访问 Spring 具体实现的更多相关文章

  1. web项目中 集合Spring&使用junit4测试Spring

    web项目中 集合Spring 问题: 如果将 ApplicationContext applicationContext = new ClassPathXmlApplicationContext(& ...

  2. 06_在web项目中集成Spring

    在web项目中集成Spring 一.使用Servlet进行集成测试 1.直接在Servlet 加载Spring 配置文件 ApplicationContext applicationContext = ...

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

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

  4. web环境中的spring MVC

    1. web.xml文件的简单详解 在web环境中, spring MVC是建立在IOC容器的基础上,要了解spring mvc,首先要了解Spring IOC容器是如何在web环境中被载入并起作用的 ...

  5. Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别

    Web.xml中自动扫描Spring的配置文件及resource时classpath*:与classpath:的区别 一.Web.xml中自动扫描Spring的配置文件(applicationCont ...

  6. 如何在Web项目中配置Spring MVC

    要使用Spring MVC需要在Web项目配置文件中web.xml中配置Spring MVC的前端控制器DispatchServlet <servlet> <servlet-name ...

  7. 如何在web项目中配置Spring的Ioc容器

    在web项目中配置Spring的Ioc容器其实就是创建web应用的上下文(WebApplicationContext) 自定义要使用的IoC容器而不使用默认的XmlApplicationContext ...

  8. Axis2在Web项目中整合Spring

    一.说明: 上一篇说了Axis2与Web项目的整合(详情 :Axis2与Web项目整合)过程,如果说在Web项目中使用了Spring框架,那么又改如何进行Axis2相关的配置操作呢? 二.Axis2 ...

  9. web项目中获取spring的bean对象

    Spring是一个轻量级的控制反转(IoC)和面向切面(AOP)的容器框架,如何在程序中不通过注解的形式(@Resource.@Autowired)获取Spring配置的bean呢? Bean工厂(c ...

随机推荐

  1. [QT][SQLITE][QTDEMO]qt5.8_sqlite数据库_demo

    qt环境:5.8 数据库:sqlite //-------------------------------------- sqlite 日期 搜索 -------------------------- ...

  2. python学习之aop装饰模式

    实际开发过程当中可能要对某些方法或者流程做出改进,添加监控,添加日志记录等所以我们要去改动已有的代码,自己的或者别人的,但改动后测试不周会引发不可控的异常,aop 模式解决了这类问题引发重复代码大量积 ...

  3. 使用wlan接收器经常重新登录怎么办

    wlan接收器是一个大功率的网卡,能够接受耿远距离的无线网络,在农村和乡镇很普及,很多家庭里都是用这个装置来接受远距离的CMCC信号.但是在使用的时候会经常出现一些问题,例如我们登陆以后,还没等上网就 ...

  4. EMQ (Erlang/Enterprise/Elastic MQTT Broker)

    EMQ (Erlang/Enterprise/Elastic MQTT Broker) https://www.cnblogs.com/SteveLee/p/9843215.html MQ介绍 EMQ ...

  5. GDI与OpenGL与DirectX之间的区别

    图形编程的几种技术对比: GDI,图形设备接口,MS开发的通用的windows系统图形编程接口,功能强涉及面广,一般的编程都用它.但是用来做多媒体开发就差强人意了 OPENGL是SGI开发的一套三维图 ...

  6. 关于setdefault和defaultdict

    c参考链接:http://blog.csdn.net/real_ray/article/details/17919289 defaultdict就是为没有的键给一个默认的值,实际是实现了一个__mis ...

  7. log框架集成

    使用slf4j,slf4j相当于一个接口,我们面对接口编程,方便地集成其他的日志框架,我们按照slf4j的标准,日志就会相应地打入日志系统中(log4j 使用slf4j要有两个包1,他本身的api,2 ...

  8. JavaWeb中验证码的实现

    在Web程序中,验证码是经常使用的技术之一.Web程序永远面临未知用户和未知程序的探测.为了防止恶意脚本的执行,验证码技术无疑是首选方案之一.本文将讨论如何在JSP和Servlet中使用验证码技术. ...

  9. Box2D学习blog

    http://www.ladeng6666.com/blog/category/box2d/

  10. 事件调度器及C++中的使用

    转自:http://blog.ch-wind.com/ue4-event-dispatcher-and-delegate/ 事件调度器非常的适合在各个蓝图之间实现通信功能. 当前UE4版本4.8.3. ...