Spring框架总结(十二)
问题引入:
程序的“事务控制”, 可以用aop实现! 即只需要写一次,运行时候动态植入到业务方法上。
一个业务的成功: 调用的service是执行成功的,意味着service中调用的所有的dao是执行成功的。 事务应该在Service层统一控制。
1、名词解释
细粒度的事务控制: 可以对指定的方法、指定的方法的某几行添加事务控制.(比较灵活,但开发起来比较繁琐: 每次都要开启、提交、回滚.)
粗粒度的事务控制: 只能给整个方法应用事务,不可以对方法的某几行应用事务。(因为aop拦截的是方法。)
编程式事务控制
程序员手动控制事务,就叫做编程式事务控制。
声明式事务控制
Spring提供了对事务的管理, 这个就叫声明式事务管理。
Spring提供了对事务控制的实现。用户如果想用Spring的声明式事务管理,只需要在配置文件中配置即可; 不想使用时直接移除配置。这个实现了对事务控制的最大程度的解耦。 Spring声明式事务管理,核心实现就是基于Aop。
编程式事务控制与声明式事务控制实现
2、编程式事务控制
Jdbc代码:
Conn.setAutoCommite(false); // 设置手动控制事务
Hibernate代码:
Session.beginTransaction(); // 开启一个事务
声明式事务控制实现
Spring声明式事务管理器类:
Jdbc技术:DataSourceTransactionManager
Hibernate技术:HibernateTransactionManager
3、声明式事务管理
步骤:
1) 引入spring-aop相关的4个jar文件
spring-aop\aopalliance.jar
aspectjrt.jar
aspectjweaver.jar
spring-aop-3.2.5.RELEASE.jar
2) 引入aop名称空间 【XML配置方式需要引入】
3) 引入tx名称空间 【事务方式必须引入】
事例编辑
1、bean.xml(注意:
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"
加载了这几个schme和相关的命名
)
<?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:p="http://www.springframework.org/schema/p"
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/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">
<!-- 数据对像.c3p0连接池 -->
<bean id="dataSource" class="com.mchange.v2.c3p0.ComboPooledDataSource">
<property name="driverClass" value="com.mysql.jdbc.Driver"></property>
<property name="jdbcUrl" value="jdbc:mysql:///springmvcdb"></property>
<property name="user" value="root"></property>
<property name="password" value="123456"></property>
<property name="initialPoolSize" value="3"></property>
<property name="maxPoolSize" value="10"></property>
<property name="maxStatements" value="100"></property>
<property name="acquireIncrement" value="2"></property>
</bean> <!-- jDBCTemplate工具类实例 -->
<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- dao实现 -->
<bean id="deptDao" class="com.liuyang.xml.action.Dept_Dao">
<property name="jdbcTemplate" ref="jdbcTemplate"></property>
</bean> <!-- service实现 -->
<bean id="deptService" class="com.liuyang.xml.action.Dept_Service">
<property name="dept_Dao" ref="deptDao"></property>
</bean> <!-- #############5. Spring声明式事务管理配置############### -->
<!-- 5.1 配置事务管理器类 -->
<bean id="txManager"
class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<property name="dataSource" ref="dataSource"></property>
</bean> <!-- 5.2 配置事务增强(如果管理事务?) -->
<tx:advice id="txAdvice" transaction-manager="txManager">
<tx:attributes>
<tx:method name="*" read-only="false" />
</tx:attributes>
</tx:advice> <!-- 5.3 Aop配置: 拦截哪些方法(切入点表表达式) + 应用上面的事务增强配置 -->
<aop:config>
<aop:pointcut
expression="execution(* com.liuyang.xml.action.Dept_Service.ServiceSave(..))"
id="pt" />
<aop:advisor advice-ref="txAdvice" pointcut-ref="pt" />
</aop:config> </beans>
2、创建user
public class Dept_user {
private int deptId;
private String deptName;
public int getDeptId() {
return deptId;
}
public void setDeptId(int deptId) {
this.deptId = deptId;
}
public String getDeptName() {
return deptName;
}
public void setDeptName(String deptName) {
this.deptName = deptName;
}
}
3、创建dao
public class Dept_Dao {
private JdbcTemplate jdbcTemplate;
public void setJdbcTemplate(JdbcTemplate jdbcTemplate) {
this.jdbcTemplate = jdbcTemplate;
}
public void save(Dept_user dept_action) {
String sql = "insert into t_dept(deptName) values(?);";
jdbcTemplate.update(sql, dept_action.getDeptName());
}
}
4、创建service
public class Dept_Service {
private Dept_Dao dept_Dao;
public void setDept_Dao(Dept_Dao dept_Dao) {
this.dept_Dao = dept_Dao;
}
public void ServiceSave(Dept_user dept_action) {
dept_Dao.save(dept_action);
}
}
5、创建测试的数据在数据库
SET FOREIGN_KEY_CHECKS=0; -- ----------------------------
-- Table structure for `t_dept`
-- ----------------------------
DROP TABLE IF EXISTS `t_dept`;
CREATE TABLE `t_dept` (
`deptId` varchar(10) NOT NULL DEFAULT '',
`deptName` varchar(50) DEFAULT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8; -- ----------------------------
-- Records of t_dept
-- ----------------------------
INSERT INTO `t_dept` VALUES ('', 'test');
INSERT INTO `t_dept` VALUES ('', '市场部');
INSERT INTO `t_dept` VALUES ('', '财务部');
INSERT INTO `t_dept` VALUES ('', '应用开发部');
INSERT INTO `t_dept` VALUES ('', 'test');
INSERT INTO `t_dept` VALUES ('', 'test');
6、测试
@SuppressWarnings("resource")
public class App {
@Test
public void testApp() throws Exception {
ApplicationContext ac = new ClassPathXmlApplicationContext(
"com/liuyang/xml/action/beanTemplate.xml");
Dept_user dept = new Dept_user();
dept.setDeptId(15);
dept.setDeptName("王小二");
Dept_Service deptService = (Dept_Service) ac.getBean("deptService");
deptService.ServiceSave(dept);
}
}
加上事物以后,遇到错误异常,事物会不将数据插入到表格中,比如
public void ServiceSave(Dept_user dept_action) {
dept_Dao.save(dept_action);
int i = 1/0;
dept_Dao.save(dept_action);
}
方法中加入如上设置,事物就不会回滚.
Spring框架总结(十二)的更多相关文章
- spring boot / cloud (十二) 异常统一处理进阶
spring boot / cloud (十二) 异常统一处理进阶 前言 在spring boot / cloud (二) 规范响应格式以及统一异常处理这篇博客中已经提到了使用@ExceptionHa ...
- Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin【Finchley 版】
Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin[Finchley 版] 发表于 2018-04-24 | 随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请 ...
- Spring学习记录(十二)---AOP理解和基于注解配置
Spring核心之二:AOP(Aspect Oriented Programming) --- 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...
- [ SSH框架 ] Spring框架学习之二(Bean的管理和AOP思想)
一.Spring的Bean管理(注解方式) 1.1 什么是注解 要使用注解方式实现Spring的Bean管理,首先要明白什么是注解.通俗地讲,注解就是代码里的特殊标记,使用注解可以完成相应功能. 注解 ...
- Spring Cloud第十二篇 | 消息总线Bus
本文是Spring Cloud专栏的第十二篇文章,了解前十一篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring ...
- spring框架学习(二)依赖注入
spring框架为我们提供了三种注入方式,分别是set注入,构造方法注入,接口注入.接口注入不作要求,下面介绍前两种方式. 1,set注入 采用属性的set方法进行初始化,就成为set注入. 1)给普 ...
- (转) Spring框架笔记(二十五)——NamedParameterJdbcTemplate与具名参数(转)
在经典的 JDBC 用法中, SQL 参数是用占位符 ? 表示,并且受到位置的限制. 定位参数的问题在于, 一旦参数的顺序发生变化, 就必须改变参数绑定. 在 Spring JDBC 框架中, 绑定 ...
- Spring框架系列(二)之Bean的注解管理
微信公众号:compassblog 欢迎关注.转发,互相学习,共同进步! 有任何问题,请后台留言联系! 1.Spring中的两种容器 在系列(一)中我们已经知道,Spring 是管理对象的容器,其中有 ...
- Spring Boot(十二)单元测试JUnit
一.介绍 JUnit是一款优秀的开源Java单元测试框架,也是目前使用率最高最流行的测试框架,开发工具Eclipse和IDEA对JUnit都有很好的支持,JUnit主要用于白盒测试和回归测试. 白盒测 ...
- 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十二 || 三种跨域方式比较,DTOs(数据传输对象)初探
更新反馈 1.博友@落幕残情童鞋说到了,Nginx反向代理实现跨域,因为我目前还没有使用到,给忽略了,这次记录下,为下次补充.此坑已填 2.提示:跨域的姊妹篇——<三十三║ ⅖ 种方法实现完美跨 ...
随机推荐
- Oracle归档与非归档模式
一.什么是Oracle归档模式 Oracle数据库有联机重做日志,这个日志是记录对数据库所做的修改,比如插入,删除,更新数据等,对这些操作都会记录在联机重做日志里.一般数据库至少要有2个联机重做日志组 ...
- 为什么要用webUI?
先看看身边有哪些软件已经在用webUI: 1.QQ查找窗口: 2.LOL主界面: 3.EC营销软件功能界面: 三个例子足以说明一切: 1.HTML是目前在用户体验.界面舒适度最先进的语言 2.HTML ...
- redis改密码
一. 如何初始化redis的密码? 总共2个步骤: a.在配置文件中有个参数: requirepass 这个就是配置redis访问密码的参数. 比如 requirepass test123 b.配置 ...
- 弗雷塞斯 从生物学到生物信息学到机器学习 转录组入门(3):了解fastq测序数据
sra文件转换为fastq格式 1 fastq-dump -h --split-3 也就是说如果SRA文件中只有一个文件,那么这个参数就会被忽略.如果原文件中有两个文件,那么它就会把成对的文件按*_1 ...
- Tkinter Cursors
Tkinter Cursors: Python的Tkinter的支持很多不同的鼠标光标的数字.确切的图形可能会有所不同,根据您的操作系统. 这里是大量有趣的的名单: "arrow&q ...
- react-router4 嵌套路由
先直接贴代码 import React from 'react'; import ReactDOM from 'react-dom'; import { HashRouter as Router, R ...
- oringin 画图
oringin做图输出矢量图方法: 右击图区,选择copy page 在Word文档中直接粘贴即可 oringin做图调整图边距: tool->option->page->margi ...
- python web框架简介Bottle Flask Tornado
Bottle Bottle是一个快速.简洁.轻量级的基于WSIG的微型Web框架,此框架只由一个 .py 文件,除了Python的标准库外,其不依赖任何其他模块. ? 1 2 3 4 pip inst ...
- i++的原子性问题
一.什么是原子性 简单的可以理解为:操作是不可再分割的,比如: int i=0; 但是i++的操作是可以再分的,比如: i++ //分解后 i=i+i 上面的代码在多线程环境下取值是有问题的,比如: ...
- Python常用的一些内建函数和math模块函数
一:Python内建函数 # abs取绝对值 num = -10 print(abs(num)) # max 求最大值 print(max(6, 9, 2, 12, 8)) # min求最小值 pri ...