问题引入:
      程序的“事务控制”, 可以用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框架总结(十二)的更多相关文章

  1. spring boot / cloud (十二) 异常统一处理进阶

    spring boot / cloud (十二) 异常统一处理进阶 前言 在spring boot / cloud (二) 规范响应格式以及统一异常处理这篇博客中已经提到了使用@ExceptionHa ...

  2. Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin【Finchley 版】

    Spring Cloud(十二):分布式链路跟踪 Sleuth 与 Zipkin[Finchley 版]  发表于 2018-04-24 |  随着业务发展,系统拆分导致系统调用链路愈发复杂一个前端请 ...

  3. Spring学习记录(十二)---AOP理解和基于注解配置

    Spring核心之二:AOP(Aspect Oriented Programming) --- 面向切面编程,通过预编译方式和运行期动态代理实现程序功能的统一维护的一种技术.AOP是OOP的延续,是软 ...

  4. [ SSH框架 ] Spring框架学习之二(Bean的管理和AOP思想)

    一.Spring的Bean管理(注解方式) 1.1 什么是注解 要使用注解方式实现Spring的Bean管理,首先要明白什么是注解.通俗地讲,注解就是代码里的特殊标记,使用注解可以完成相应功能. 注解 ...

  5. Spring Cloud第十二篇 | 消息总线Bus

    ​ ​本文是Spring Cloud专栏的第十二篇文章,了解前十一篇文章内容有助于更好的理解本文: Spring Cloud第一篇 | Spring Cloud前言及其常用组件介绍概览 Spring ...

  6. spring框架学习(二)依赖注入

    spring框架为我们提供了三种注入方式,分别是set注入,构造方法注入,接口注入.接口注入不作要求,下面介绍前两种方式. 1,set注入 采用属性的set方法进行初始化,就成为set注入. 1)给普 ...

  7. (转) Spring框架笔记(二十五)——NamedParameterJdbcTemplate与具名参数(转)

    在经典的 JDBC 用法中, SQL 参数是用占位符 ? 表示,并且受到位置的限制. 定位参数的问题在于, 一旦参数的顺序发生变化, 就必须改变参数绑定. 在 Spring JDBC 框架中, 绑定 ...

  8. Spring框架系列(二)之Bean的注解管理

    微信公众号:compassblog 欢迎关注.转发,互相学习,共同进步! 有任何问题,请后台留言联系! 1.Spring中的两种容器 在系列(一)中我们已经知道,Spring 是管理对象的容器,其中有 ...

  9. Spring Boot(十二)单元测试JUnit

    一.介绍 JUnit是一款优秀的开源Java单元测试框架,也是目前使用率最高最流行的测试框架,开发工具Eclipse和IDEA对JUnit都有很好的支持,JUnit主要用于白盒测试和回归测试. 白盒测 ...

  10. 从壹开始前后端分离【 .NET Core2.0 +Vue2.0 】框架之十二 || 三种跨域方式比较,DTOs(数据传输对象)初探

    更新反馈 1.博友@落幕残情童鞋说到了,Nginx反向代理实现跨域,因为我目前还没有使用到,给忽略了,这次记录下,为下次补充.此坑已填 2.提示:跨域的姊妹篇——<三十三║ ⅖ 种方法实现完美跨 ...

随机推荐

  1. Linut ssh sftp服务重启

    在网上,收了半天,终于找到这个,记录一下~哈~ RedHat Linux 重启SSH /etc/init.d/sshd restart 重启SFTP /etc/init.d/vsftpd restar ...

  2. 解决: Project facet Java version 1.8 is not supported

    背景 从别处Import一个Java project之后,Eclipse提示“Project facet Java version 1.8 is not supported”. 分析 从错误的描述来看 ...

  3. 第六章 通过Service访问Pod(下)

    6.4 外网如何访问service (1)ClusterIp: Service通过Cluster内部的IP对外提供服务,只有Cluster内的节点和Pod可以访问,这是默认的Service类型. (2 ...

  4. JS回调函数(深入篇)

    <有些错别字> 在Javascript中,函数是第一类对象,这意味着函数可以像对象一样按照第一类管理被使用.既然函数实际上是对象:它们能被“存储”在变量中,能作为函数参数被传递,能在函数中 ...

  5. Android:ScrollView和SwipeRefreshLayout高度测量

    今天组里的同事要做一个奇葩的效果,要求在ScrollView里嵌套一个RefreshLayout.类似代码如下: <?xml version="1.0" encoding=& ...

  6. msyqld 的 The user specified as a definer ('root'@'%') does not exist 问题

    msyqld 的 The user specified as a definer ('root'@'%') does not exist 问题 造成问题:搭建网站时显示内容不完整. 跟踪tomcat日 ...

  7. Tomcat官方文档关于数据源配置的内容

    虽然有网上有网友自己总结的文章,但说明得总是不够清晰,还是参考官方文档理解得比较透彻: http://tomcat.apache.org/tomcat-7.0-doc/jdbc-pool.html h ...

  8. vue之slot,组件标签嵌套

    vue之slot,组件标签嵌套 插槽(Slot),在各种vue的ui插件中,经常见到的多个组件标签相互嵌套(如下)就是以此为基础的. <el-col > <el-checkbox & ...

  9. 从邮件原理来看 postfix和docecot

    好早好早以前计算机网络老师就教了说,邮件嘛,就三个协议smtp,imap,pop3. smtp 用来发邮件,imap,pop3用来收邮件.噢?是么.难道没有发现这句话有非常多的漏洞,根本就不能说清楚这 ...

  10. unit_2_homework

    随记2018/4/23 # 找元祖中的元素,移除每个元素的空格,并查找以a或A开头,c结尾的所有元素. # 思路:将i取出来,求得li列表中有多少个元素for i in range(len(li)): ...