基于XML的声明式事务控制
1、maven依赖
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>com.ly.spring</groupId>
<artifactId>spring07</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>jar</packaging> <dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--JdbcTemplate-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.0.2.RELEASE</version>
</dependency>
<!--整合junit-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>5.0.2.RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<version>5.1.6</version>
</dependency>
<!--解析切入点表达式-->
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.7</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
</dependencies> <!--解决IDEA maven变更后自动重置LanguageLevel和JavaCompiler版本的问题-->
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>13</source>
<target>13</target>
</configuration>
</plugin>
</plugins>
</build>
</project>
2、abc服务和实现类
package com.ly.spring.service;
public interface IAbcService {
public void updateAbcMoney(String abcBankNo,double addMoney);
}
package com.ly.spring.service.impl; import com.ly.spring.dao.IAbcDao;
import com.ly.spring.service.IAbcService; public class AbcServiceImpl implements IAbcService {
private IAbcDao abcDao; public void setAbcDao(IAbcDao abcDao) {
this.abcDao = abcDao;
} @Override
public void updateAbcMoney(String abcBankNo, double addMoney) {
abcDao.updateAbcMoney(abcBankNo,addMoney);
}
}
3、abc dao和实现类
package com.ly.spring.dao;
public interface IAbcDao {
public void updateAbcMoney(String abcBankNo,double addMoney);
}
package com.ly.spring.dao.impl; import com.ly.spring.dao.IAbcDao;
import org.springframework.jdbc.core.support.JdbcDaoSupport; public class AbcDaoImpl extends JdbcDaoSupport implements IAbcDao {
@Override
public void updateAbcMoney(String abcBankNo, double addMoney) {
super.getJdbcTemplate().update("update abc set money = money+? where bankno = ?",addMoney,abcBankNo);
}
}
4、icbc服务和实现类
package com.ly.spring.service;
public interface IIcbcService {
public void updateIcbcMoney(String icbcBankNo, double addMoney);
}
package com.ly.spring.service.impl; import com.ly.spring.dao.IIcbcDao;
import com.ly.spring.service.IIcbcService; public class IcbcServiceImpl implements IIcbcService {
private IIcbcDao icbcDao; public void setIcbcDao(IIcbcDao icbcDao) {
this.icbcDao = icbcDao;
} @Override
public void updateIcbcMoney(String icbcBankNo, double addMoney) {
icbcDao.updateIcbcMoney(icbcBankNo,addMoney);
}
}
5、icbc dao和实现类
package com.ly.spring.dao;
public interface IIcbcDao {
public void updateIcbcMoney(String icbcBankNo, double addMoney);
}
package com.ly.spring.dao.impl; import com.ly.spring.dao.IIcbcDao;
import org.springframework.jdbc.core.support.JdbcDaoSupport; public class IcbcDaoImpl extends JdbcDaoSupport implements IIcbcDao {
@Override
public void updateIcbcMoney(String icbcBankNo, double addMoney) {
super.getJdbcTemplate().update("update icbc set money = money+? where bankno = ?",addMoney,icbcBankNo);
}
}
6、转账服务和实现类
package com.ly.spring.service; /**
* 转账服务
*/
public interface ITransferAccountService {
public void transferAccountFromIcbcToAbc(String icbcBankNo,String abcBankNo,double money);
}
package com.ly.spring.service.impl; import com.ly.spring.service.IAbcService;
import com.ly.spring.service.IIcbcService;
import com.ly.spring.service.ITransferAccountService; public class TransferAccountServiceImpl implements ITransferAccountService {
private IIcbcService icbcService;
private IAbcService abcService; public void setIcbcService(IIcbcService icbcService) {
this.icbcService = icbcService;
} public void setAbcService(IAbcService abcService) {
this.abcService = abcService;
} @Override
public void transferAccountFromIcbcToAbc(String icbcBankNo, String abcBankNo, double money) {
//先扣减icbc
icbcService.updateIcbcMoney(icbcBankNo,0-money);
//再增加abc
abcService.updateAbcMoney(abcBankNo,money);
//int i = 1/0;
}
}
7、jdbc资源文件
jdbc.driver = com.mysql.jdbc.Driver
jdbc.url = jdbc:mysql://localhost:3306/db01
jdbc.user = root
jdbc.password = root
8、spring配置文件
<?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:tx="http://www.springframework.org/schema/tx"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.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
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd">
<!--配置jdbc资源文件-->
<context:property-placeholder location="classpath:jdbc.properties"></context:property-placeholder>
<!--转账服务-->
<bean id="transferAccountService" class="com.ly.spring.service.impl.TransferAccountServiceImpl">
<property name="abcService" ref="abcService"></property>
<property name="icbcService" ref="icbcService"></property>
</bean>
<!--icbc服务-->
<bean id="icbcService" class="com.ly.spring.service.impl.IcbcServiceImpl">
<property name="icbcDao" ref="icbcDao"></property>
</bean>
<!--abc服务-->
<bean id="abcService" class="com.ly.spring.service.impl.AbcServiceImpl">
<property name="abcDao" ref="abcDao"></property>
</bean>
<!--icbc dao-->
<bean id="icbcDao" class="com.ly.spring.dao.impl.IcbcDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--abc dao-->
<bean id="abcDao" class="com.ly.spring.dao.impl.AbcDaoImpl">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置数据源-->
<bean id="dataSource" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="${jdbc.driver}"></property>
<property name="url" value="${jdbc.url}"></property>
<property name="username" value="${jdbc.user}"></property>
<property name="password" value="${jdbc.password}"></property>
</bean>
<!--配置事务管理器-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean>
<!--配置事务通知和事务通知的属性-->
<tx:advice id="transactionAdvice" transaction-manager="transactionManager">
<tx:attributes>
<tx:method name="*" propagation="REQUIRED" read-only="false"/>
<tx:method name="find*" propagation="SUPPORTS" read-only="true"></tx:method>
</tx:attributes>
</tx:advice>
<!--aopp欸之-->
<aop:config>
<!--配置切入点-->
<aop:pointcut id="pt1" expression="execution(* com.ly.spring.service.impl.*.*(..))"/>
<!--将事务通知和切入点关联起来-->
<aop:advisor advice-ref="transactionAdvice" pointcut-ref="pt1"></aop:advisor>
</aop:config>
</beans>
9、测试类
package com.ly.spring.test; import com.ly.spring.service.ITransferAccountService;
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;
//替换junit的main方法
@RunWith(SpringJUnit4ClassRunner.class)
//指定spring配置文件位置
@ContextConfiguration(locations = "classpath:bean.xml")
public class MainTest {
@Autowired
private ITransferAccountService transferAccountService;
@Test
public void test() {
transferAccountService.transferAccountFromIcbcToAbc("1234567890","1001",500);
}
}
基于XML的声明式事务控制的更多相关文章
- spring基于XML的声明式事务控制
<?xml version="1.0" encoding="utf-8" ?><beans xmlns="http://www.sp ...
- spring基于xml的声明式事务控制配置步骤
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- 阶段3 2.Spring_10.Spring中事务控制_6 spring基于XML的声明式事务控制-配置步骤
环境搭建 新建工程 把对应的依赖复制过来 src下内容复制 配置spring中的声明事物 找到bean.xml开始配置 配置事物管理器 里面需要注入DataSource 2-配置事物通知 需要先导入事 ...
- Spring中基于XML的声明式事务控制配置步骤
1.配置事务管理器 2.配置事务的通知 此时,我们就需要导入事务的约束 tx名称空间和约束,同时也需要aop的 使用tx:advice标签配置事务通知 属性: id:给事务通知起一个唯一标识 tran ...
- spring基于注解的声明式事务控制
package com.hope.service.impl;import com.hope.dao.IAccountDao;import com.hope.domain.Account;import ...
- 阶段3 2.Spring_10.Spring中事务控制_7 spring基于注解的声明式事务控制
创建新项目 复制上一个pom.xml的内容.依赖和打包的方式 再复制src的代码过来 bean.xml.多导入context的声明 Service的实现类增加注解 dao的set方法删掉 通过Auto ...
- 阶段3 2.Spring_10.Spring中事务控制_1 基于XML的AOP实现事务控制
新建项目 首先把依赖复制进来 aop必须引入.aspectjweaver 复制src下的所有内容 复制到我们的新项目里面 factory文件夹删掉 删除后测试类必然就报错 配置文件 beanFacto ...
- 阶段3 2.Spring_10.Spring中事务控制_8 spring基于纯注解的声明式事务控制
新建项目 把之前项目src下的内容全部复制过来 pom.xml内复制过来 开始配置 新建一个config的包,然后再新建配置文件类SpringConfiguration @Configuration这 ...
- 28Spring_的事务管理_银行转账业务加上事务控制_基于注解进行声明式事务管理
将applicationContext.xml 和 AccountServiceImpl 给备份一个取名为applicationContext2.xml 和 AccountServiceImpl2.j ...
随机推荐
- 物流跟踪API-快递单订阅
上一篇文章我们讲解了轨迹查询的接口,通过快递鸟接口可以实现实时查询物流轨迹,这次给大家推荐订阅服务功能. 为了更好的理解订阅服务,我们来做个对比, 即时查询是主动查询物流轨迹,需要我们主动调用接口才能 ...
- C编程规范
目 录 1.版面... 2.命名... 3.注释... 4.源代码结构... 附录A:常见单词缩写表... 1.版面 [规则1-1] 程序块要采用缩进风格编写,缩进的空格数为4个. [规则1-2] 对 ...
- Windows搭建IIS服务器使用NATAPP实现内网穿透
目的:外网可以访问本地网页. 步骤: 一.实现内网访问 1.Win+Q搜索[控制面板],选择[程序],点击[启用或关闭Windows功能], 2.勾选[Internet Information Ser ...
- 使用docker19.03.6部署zabbix
可参考官方文档:https://www.zabbix.com/documentation/4.0/zh/manual/installation/containers 1)启动一个空的mysql服务器实 ...
- geo常见需求
常见的地理位置相关需求有: 1.查找附近的人 2.显示两点距离 3.点是否在指定范围内(地理围栏) redis.MongoDB.mysql都已支持geo 几种geo方案对比 https://blog. ...
- Windows 远程桌面连接Ubuntu14.04
在Ubuntu系统进行如下系统配置 1.安装xrdp sudo apt-get install xrdp 2.安装vnc4server sudo apt-get install vnc4server ...
- [Redis-CentOS7]Redis事务操作(六)
事务操作 隔离操作: 事务中所有的命令都会序列化,按顺序执行,不会被其他命令打扰 原子操作: 事务中所有的命令要么全部执行,要么全部不执行 添加事务并执行 127.0.0.1:6379> MUL ...
- 1058 - Parallelogram Counting 计算几何
1058 - Parallelogram Counting There are n distinct points in the plane, given by their integer coord ...
- Java 添加、读取、删除Excel图片
本文介绍在Java程序中如何添加图片到excel表格,添加图片时可设置图片大小.位置.旋转.超链接.可选文本等,以及如何读取.删除excel表格中已有的图片. 工具:Free Spire.XLS fo ...
- MySQL常用语法总结
一,学习mysql的前戏 1:基础入门命令 show databases: #查看当前MySQL中的所有数据库 create 数据库名: #创建新的数据库 use 数据库名: #使用该数据库 show ...