spring中使用aop配置事务
spring的事务配置有5种方式,这里记录其中的一种:基于tx/aop声明式事务配置
在之前spring aop介绍和示例这篇所附代码的基础上进行测试
一、添加save方法
1、在testDao类里添加方法:
- /**
- * 保存实体
- */
- public void save(testInfo info) {
- sessionFactory.getCurrentSession().save(info);
- }
2、在HelloController类里添加方法:
- /**
- * 保存实体
- * @param request
- * @return
- */
- @RequestMapping("save")
- public String save(HttpServletRequest request) {
- testInfo info = new testInfo();
- info.setId(request.getParameter("id"));
- info.setName(request.getParameter("name"));
- testDao.save(info);
- return "redirect:/hello/mysql";
- }
二、添加页面
1、添加jar包引用,修改pom.xml文件,加入页面标签的支持:
- <!-- web -->
- <dependency>
- <groupId>taglibs</groupId>
- <artifactId>standard</artifactId>
- <version>1.1.2</version>
- </dependency>
- <dependency>
- <groupId>javax.servlet</groupId>
- <artifactId>jstl</artifactId>
- <version>1.2</version>
- </dependency>
2、修改index3.jsp页面,加入forEach标签:
- <%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%>
- <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
- <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
- "http://www.w3.org/TR/html4/loose.dtd">
- <html>
- <head>
- <meta http-equiv="Content-Type" content="text/html;charset=utf-8" />
- <title>Insert title here</title>
- </head>
- <body>
- <p>${say}</p>
- <c:if test="${!empty testList}">
- <table border="1" width="100px">
- <tr>
- <th>列1</th>
- <th>列2</th>
- </tr>
- <c:forEach items="${testList}" var="item">
- <tr>
- <td>${item.id}</td>
- <td>${item.name}</td>
- </tr>
- </c:forEach>
- </table>
- </c:if>
- </body>
- </html>
三、不使用事务运行测试
浏览器访问"http://localhost:8080/demo1/hello/save?id=3&name=123",页面显示:
发现保存后数据没有变化,因为hibernate中如果没有开启事务,在做增删改的时候一直是未提交状态,下面加入事务再进行测试:
四、加入tx/aop事务配置
在spring-context-hibernate.xml配置中添加:
- <!-- 定义事务管理器(声明式的事务) -->
- <bean id="transactionManager" class="org.springframework.orm.hibernate4.HibernateTransactionManager">
- <property name="sessionFactory" ref="sessionFactory" />
- </bean>
- <!-- 声明式容器事务管理 -->
- <tx:advice id="txAdvice" transaction-manager="transactionManager">
- <tx:attributes>
- <tx:method name="save*" propagation="REQUIRED" />
- <tx:method name="*" read-only="true" />
- </tx:attributes>
- </tx:advice>
- <!-- 定义切面 -->
- <aop:config expose-proxy="true">
- <!-- 只对业务逻辑层实施事务 -->
- <aop:pointcut id="txPointcut" expression="execution(* org.xs.demo1.testDao.*(..))" />
- <!-- Advisor定义,切入点和通知分别为txPointcut、txAdvice -->
- <aop:advisor pointcut-ref="txPointcut" advice-ref="txAdvice"/>
- </aop:config>
五、使用事务后运行测试
浏览器访问"http://localhost:8080/demo1/hello/save?id=3&name=123",页面显示:
添加成功!
实例代码地址:https://github.com/ctxsdhy/cnblogs-example
spring中使用aop配置事务的更多相关文章
- spring中的AOP 以及各种通知 配置
理解了前面动态代理对象的原理之后,其实还是有很多不足之处,因为如果在项目中有20多个类,每个类有100多个方法都需要判断是不是要开事务,那么方法调用那里会相当麻烦. spring中的AOP很好地解决了 ...
- Spring中的AOP 专题
Caused by: java.lang.IllegalArgumentException: ProceedingJoinPoint is only supported for around advi ...
- Spring入门4.AOP配置深入
Spring入门4.AOP配置深入 代码下载 链接: http://pan.baidu.com/s/11mYEO 密码: x7wa 前言: 之前学习AOP中的一些概念,包括连接点.切入点(pointc ...
- Spring学习笔记(四)—— Spring中的AOP
一.AOP概述 AOP(Aspect Oriented Programming),即面向切面编程,可以说是OOP(Object Oriented Programming,面向对象编程)的补充和完善.O ...
- spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式
spring+hibernate 配置多个数据源过程 以及 spring中数据源的配置方式[部分内容转载] 2018年03月27日 18:58:41 守望dfdfdf 阅读数:62更多 个人分类: 工 ...
- JavaWeb_(Spring框架)认识Spring中的aop
1.aop思想介绍(面向切面编程):将纵向重复代码,横向抽取解决,简称:横切 2.Spring中的aop:无需我们自己写动态代理的代码,spring可以将容器中管理对象生成动态代理对象,前提是我们对他 ...
- Spring中的AOP
什么是AOP? (以下内容来自百度百科) 面向切面编程(也叫面向方面编程):Aspect Oriented Programming(AOP),通过预编译方式和运行期动态代理实现程序功能的统一维护的一种 ...
- 2018.12.24 Spring中的aop演示(也就是运用aop技术实现代理模式)
Aop的最大意义是:在不改变原来代码的前提下,也不对源代码做任何协议接口要求.而实现了类似插件的方式,来修改源代码,给源代码插入新的执行代码. 1.spring中的aop演示 aop:面向方面编程.不 ...
- (五)Spring 中的 aop
目录 文章目录 AOP概念 AOP原理 AOP术语 **`Spring`** 中的 **`aop`** 的操作 使用 `AspectJ` 实现 `aop` 的两种方式 AOP概念 浅理解 aop :面 ...
随机推荐
- gin+vue的前后端分离开源项目
该项目是gin+vue的前后端分离项目,使用gorm访问MySQL,其中vue前端是使用vue-element-admin框架简单实现的: go后台使用jwt,对API接口进行权限控制.此外,Web页 ...
- 关于python中str数据类型的内置常用方法(函数)总结
str基本数据类型常用功能 center(self,width,fllchar=none) 内容居中,width表示总长度,fllchar表示空白处默认为 ...
- 以帧为存储单位的循环stack
此stack主要是作为存储空间使用,主要的借口就是push和pop. stack frame的src以及例程位于stack_FrameTest这个库当中,其中有readme文件,可以快速上手. sta ...
- java 正则 替换中文为空
//中文替换为"" public String replaceChineseToNULL(String s){ String reg = "[\u4e00-\u9fa5] ...
- Linux网络问题排错
前言 作为一名软件工程师,Linux相关的知识是一个不可或缺的技能点,而网络问题往往是初学者接触Linux时最先碰到的一只拦路虎,本篇博客将系统的讲解一个解决Linux网络问题的通用方法论,一个科学的 ...
- Android进阶之绘制-自定义View完全掌握(一)
Android的UI设计可以说是决定一个app质量的关键因素,因为人们在使用app的时候,最先映入眼帘的就是app的界面了,一个美观.充实的界面能够给用户带来非常好的体验,会在用户心中留下好的印象. ...
- 【转】[Python小记] 通俗的理解闭包 闭包能帮我们做什么?
https://blog.csdn.net/sc_lilei/article/details/80464645
- 学生管理系统 Python语言
def show_student(): print(('*'*20).center(55)) print('1.添加学生信息'.center(50)) print('2.修改学生信息'.center( ...
- Keras(四)CNN 卷积神经网络 RNN 循环神经网络 原理及实例
CNN 卷积神经网络 卷积 池化 https://www.cnblogs.com/peng8098/p/nlp_16.html 中有介绍 以数据集MNIST构建一个卷积神经网路 from keras. ...
- 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 ...