spring的事务配置有5种方式,这里记录其中的一种:基于tx/aop声明式事务配置

在之前spring aop介绍和示例这篇所附代码的基础上进行测试

一、添加save方法

1、在testDao类里添加方法:

  1. /**
  2. * 保存实体
  3. */
  4. public void save(testInfo info) {
  5. sessionFactory.getCurrentSession().save(info);
  6. }

2、在HelloController类里添加方法:

  1. /**
  2. * 保存实体
  3. * @param request
  4. * @return
  5. */
  6. @RequestMapping("save")
  7. public String save(HttpServletRequest request) {
  8.  
  9. testInfo info = new testInfo();
  10. info.setId(request.getParameter("id"));
  11. info.setName(request.getParameter("name"));
  12.  
  13. testDao.save(info);
  14.  
  15. return "redirect:/hello/mysql";
  16. }

二、添加页面

1、添加jar包引用,修改pom.xml文件,加入页面标签的支持:

  1. <!-- web -->
  2. <dependency>
  3. <groupId>taglibs</groupId>
  4. <artifactId>standard</artifactId>
  5. <version>1.1.2</version>
  6. </dependency>
  7. <dependency>
  8. <groupId>javax.servlet</groupId>
  9. <artifactId>jstl</artifactId>
  10. <version>1.2</version>
  11. </dependency>

2、修改index3.jsp页面,加入forEach标签:

  1. <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
  2. <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
  3. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
  4. "http://www.w3.org/TR/html4/loose.dtd">
  5. <html>
  6. <head>
  7. <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
  8. <title>Insert title here</title>
  9. </head>
  10. <body>
  11. <p>${say}</p>
  12. <c:if test="${!empty testList}">
  13. <table border="1" width="100px">
  14. <tr>
  15. <th>列1</th>
  16. <th>列2</th>
  17. </tr>
  18. <c:forEach items="${testList}" var="item">
  19. <tr>
  20. <td>${item.id}</td>
  21. <td>${item.name}</td>
  22. </tr>
  23. </c:forEach>
  24. </table>
  25. </c:if>
  26. </body>
  27. </html>

三、不使用事务运行测试

浏览器访问"http://localhost:8080/demo1/hello/save?id=3&name=123",页面显示:

发现保存后数据没有变化,因为hibernate中如果没有开启事务,在做增删改的时候一直是未提交状态,下面加入事务再进行测试:

四、加入tx/aop事务配置

在spring-context-hibernate.xml配置中添加:

  1. <!-- 定义事务管理器(声明式的事务) -->
  2. <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
  3. <property name="sessionFactory" ref="sessionFactory" />
  4. </bean>
  5.  
  6. <!-- 声明式容器事务管理 -->
  7. <tx:advice id="txAdvice" transaction-manager="transactionManager">
  8. <tx:attributes>
  9. <tx:method name="save*" propagation="REQUIRED" />
  10. <tx:method name="*" read-only="true" />
  11. </tx:attributes>
  12. </tx:advice>
  13.  
  14. <!-- 定义切面 -->
  15. <aop:config expose-proxy="true">
  16. <!-- 只对业务逻辑层实施事务 -->
  17. <aop:pointcut id="txPointcut" expression="execution(* org.xs.demo1.testDao.*(..))" />
  18. <!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice -->
  19. <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
  20. </aop:config>

五、使用事务后运行测试

浏览器访问"http://localhost:8080/demo1/hello/save?id=3&name=123",页面显示:

添加成功!

实例代码地址:https://github.com/ctxsdhy/cnblogs-example

spring中使用aop配置事务的更多相关文章

  1. spring中的AOP 以及各种通知 配置

    理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...

  2. Spring中的AOP 专题

    Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...

  3. Spring入门4.AOP配置深入

    Spring入门4.AOP配置深入 代码下载 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 之前学习AOP中的一些概念,包括连接点.切入点(pointc ...

  4. Spring学习笔记(四)—— Spring中的AOP

    一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...

  5. spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式

    spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式[部分内容转载] 2018年03月27日 18:58:41 守望dfdfdf 阅读数:62更多 个人分类: 工 ...

  6. JavaWeb_(Spring框架)认识Spring中的aop

    1.aop思想介绍(面向切面编程):将纵向重复代码,横向抽取解决,简称:横切 2.Spring中的aop:无需我们自己写动态代理的代码,spring可以将容器中管理对象生成动态代理对象,前提是我们对他 ...

  7. Spring中的AOP

    什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...

  8. 2018.12.24 Spring中的aop演示(也就是运用aop技术实现代理模式)

    Aop的最大意义是:在不改变原来代码的前提下,也不对源代码做任何协议接口要求.而实现了类似插件的方式,来修改源代码,给源代码插入新的执行代码. 1.spring中的aop演示 aop:面向方面编程.不 ...

  9. (五)Spring 中的 aop

    目录 文章目录 AOP概念 AOP原理 AOP术语 **`Spring`** 中的 **`aop`** 的操作 使用 `AspectJ` 实现 `aop` 的两种方式 AOP概念 浅理解 aop :面 ...

随机推荐

  1. gin+vue的前后端分离开源项目

    该项目是gin+vue的前后端分离项目,使用gorm访问MySQL,其中vue前端是使用vue-element-admin框架简单实现的: go后台使用jwt,对API接口进行权限控制.此外,Web页 ...

  2. 关于python中str数据类型的内置常用方法(函数)总结

    str基本数据类型常用功能 center(self,width,fllchar=none)                        内容居中,width表示总长度,fllchar表示空白处默认为 ...

  3. 以帧为存储单位的循环stack

    此stack主要是作为存储空间使用,主要的借口就是push和pop. stack frame的src以及例程位于stack_FrameTest这个库当中,其中有readme文件,可以快速上手. sta ...

  4. java 正则 替换中文为空

    //中文替换为"" public String replaceChineseToNULL(String s){ String reg = "[\u4e00-\u9fa5] ...

  5. Linux网络问题排错

    前言 作为一名软件工程师,Linux相关的知识是一个不可或缺的技能点,而网络问题往往是初学者接触Linux时最先碰到的一只拦路虎,本篇博客将系统的讲解一个解决Linux网络问题的通用方法论,一个科学的 ...

  6. Android进阶之绘制-自定义View完全掌握(一)

    Android的UI设计可以说是决定一个app质量的关键因素,因为人们在使用app的时候,最先映入眼帘的就是app的界面了,一个美观.充实的界面能够给用户带来非常好的体验,会在用户心中留下好的印象. ...

  7. 【转】[Python小记] 通俗的理解闭包 闭包能帮我们做什么?

    https://blog.csdn.net/sc_lilei/article/details/80464645

  8. 学生管理系统 Python语言

    def show_student(): print(('*'*20).center(55)) print('1.添加学生信息'.center(50)) print('2.修改学生信息'.center( ...

  9. Keras(四)CNN 卷积神经网络 RNN 循环神经网络 原理及实例

    CNN 卷积神经网络 卷积 池化 https://www.cnblogs.com/peng8098/p/nlp_16.html 中有介绍 以数据集MNIST构建一个卷积神经网路 from keras. ...

  10. ImportError: cannot import name '_obtain_input_shape' from 'keras.applications.imagenet_utils'

    报错 Using TensorFlow backend. Traceback (most recent call last): File "D:/PyCharm 5.0.3/WorkSpac ...