Spring AOP及事务配置三种模式详解
Spring AOP简述
Spring AOP的设计思想,就是通过动态代理,在运行期对需要使用的业务逻辑方法进行增强。
使用场景如:日志打印、权限、事务控制等。
默认情况下,Spring会根据被代理的对象是否实现接口来选择使用JDK还是CGLIB。当被代理对象没有实现接口时,Spring会选择CGLIB。当实现了接口,Spring会选择JDK官方的代理技术,不过我们也可以通过配置的方式,让Spring强制使用CGLIB。
配置方式有两种:
- 使⽤aop:config标签配置
<aop:config proxy-target-class="true">
- 使⽤aop:aspectj-autoproxy标签配置
<aop:aspectj-autoproxy proxy-target-class="true"></aop:aspectj-
autoproxy>
Spring中AOP的实现
2.1 XML模式
- 引入依赖(如果项目里没有的话)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
- xml配置
主要看下面的aop部分
<?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: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
https://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
https://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/tx
https://www.springframework.org/schema/tx/spring-tx.xsd
http://www.springframework.org/schema/aop
https://www.springframework.org/schema/aop/spring-aop.xsd
">
xml相关切面配置
<bean id="logUtil" class="com.mmc.ioc.utils.LogUtil"></bean>
<!--aop的配置-->
<!--aop的配置-->
<aop:config>
<!--配置切面-->
<aop:aspect id="logAdvice" ref="logUtil">
<aop:pointcut id="logAspect" expression="execution(public * com.mmc.ioc.service.impl.TransferServiceImpl.transfer(..))"></aop:pointcut>
<!--前置通知-->
<aop:before method="printLog" pointcut-ref="logAspect"></aop:before>
<!--后置通知,无论业务是否正常执行-->
<aop:after method="after" pointcut-ref="logAspect"></aop:after>
<!--正常执行-->
<aop:after-returning method="afterReturn" pointcut-ref="logAspect"></aop:after-returning>
<!--异常执行-->
<aop:after-throwing method="afterException" pointcut-ref="logAspect"></aop:after-throwing>
<!--环绕通知-->
<!--<aop:around method="around" pointcut-ref="logAspect" arg-names="proceedingJoinPoint"></aop:around>-->
</aop:aspect>
</aop:config>
环绕通知可以实现上面的4种通知,并且可以控制业务方法是否执行。通过如下代码控制:
proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
public class LogUtil {
public void printLog(){
System.out.println("打印日志");
}
public void after(){
System.out.println("后日志打印,不管业务是否正常");
}
public void afterReturn(){
System.out.println("正常执行完毕打印日志");
}
public void afterException(){
System.out.println("异常执行打印日志");
}
public void around(ProceedingJoinPoint proceedingJoinPoint){
System.out.println("环绕前置");
try {
Object result = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
System.out.println("环绕正常执行");
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("环绕异常执行");
}
}
}
- 切入点表达式
举例:
public void
com.lagou.service.impl.TransferServiceImpl.updateAccountByCardNo(c
om.lagou.pojo.Account)
- 访问修饰符可以省略,也就是public可以不用写
void com.mmc.ioc.service.impl.TransferServiceImpl.transfer(String,String,int)
- 返回值可以用*代替,表示返回任意值
* com.mmc.ioc.service.impl.TransferServiceImpl.transfer(String,String,int)
- 包名可以使用..表示当前包及其子包
* com..TransferServiceImpl.transfer(String,String,int)
- 类名和方法名,都可以使用*表示任意类,任意方法
* com..*(String,String,int))
- 参数列表,如果是基本类型可以直接写名称,如int。引用类型必须用全限定名称
- 参数列表可以使用*代替任意参数类型,但必须有参数
* com..*(*)
- 参数列表可以使用..代替任意参数类型,有无参数均可
* com..*(*)
- 全通配方式:
* *..*.*(..)
2.2 XML+注解模式
- XML中开启Spring对注解AOP的支持
<!--开启spring对注解aop的⽀持-->
<aop:aspectj-autoproxy/>
- 注解配置
@Component
@Aspect
public class LogUtil {
@Pointcut("execution(* com.mmc.ioc.service.impl.TransferServiceImpl.transfer(..))")
public void pointcut(){}
@Before("pointcut()")
public void printLog(){
System.out.println("打印日志");
}
@After("pointcut()")
public void after(){
System.out.println("后日志打印,不管业务是否正常");
}
@AfterReturning("pointcut()")
public void afterReturn(){
System.out.println("正常执行完毕打印日志");
}
@AfterThrowing("pointcut()")
public void afterException(){
System.out.println("异常执行打印日志");
}
// @Around("pointcut()")
public void around(ProceedingJoinPoint proceedingJoinPoint){
System.out.println("环绕前置");
try {
Object result = proceedingJoinPoint.proceed(proceedingJoinPoint.getArgs());
System.out.println("环绕正常执行");
} catch (Throwable throwable) {
throwable.printStackTrace();
System.out.println("环绕异常执行");
}
}
}
2.3 纯注解模式
只需要用注解@EnableAspectJAutoProxy替换掉
<aop:aspectj-autoproxy/>
Spring事务配置
也分为3种模式
3.1 XML模式
- 引入pom依赖
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-jdbc</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-tx</artifactId>
<version>5.1.12.RELEASE</version>
</dependency>
- xml配置
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
<tx:advice id="txAdice" transaction-manager="transactionManager">
<!--定制事务细节-->
<tx:attributes>
<tx:method name="*" read-only="false" propagation="REQUIRED" isolation="DEFAULT"/>
<tx:method name="query*" read-only="true" propagation="SUPPORTS"></tx:method>
</tx:attributes>
</tx:advice>
<!--事务衡器逻辑-->
<aop:config>
<aop:advisor advice-ref="txAdice" pointcut="execution(* com.mmc.ioc.service.impl.TransferServiceImpl.transfer(..))"></aop:advisor>
</aop:config>
3.2 基于XML+注解
- xml配置:
<!--spring声明式事务配置-->
<bean id="transactionManager" class="org.springframework.jdbc.datasource.DataSourceTransactionManager">
<constructor-arg name="dataSource" ref="dataSource"></constructor-arg>
</bean>
<tx:annotation-driven transaction-manager="transactionManager"></tx:annotation-driven>
- 在类或方法上面添加@Transactional注解
@Transactional(readOnly = true,propagation = Propagation.SUPPORTS)
3.3 纯注解
用@EnableTransactionManagement 注解替换掉
<tx:annotation-driven transaction-
manager="transactionManager"/>
即可
Spring AOP及事务配置三种模式详解的更多相关文章
- 分布式事务 Seata Saga 模式首秀以及三种模式详解 | Meetup#3 回顾
https://mp.weixin.qq.com/s/67NvEVljnU-0-6rb7MWpGw 分布式事务 Seata Saga 模式首秀以及三种模式详解 | Meetup#3 回顾 原创 蚂蚁金 ...
- Vi三种模式详解
命令行模式 (command mode/一般模式) 任何时候,不管用户处于何种模式,只要按一下“ESC”键,即可使Vi进入命令行模式:我们在shell环境(提示符为$)下输入启动Vi命令,进入编辑器时 ...
- spring AOP 之四:@AspectJ切入点标识符语法详解
@AspectJ相关文章 <spring AOP 之二:@AspectJ注解的3种配置> <spring AOP 之三:使用@AspectJ定义切入点> <spring ...
- rabbitmq五种模式详解(含实现代码)
一.五种模式详解 1.简单模式(Queue模式) 当生产端发送消息到交换机,交换机根据消息属性发送到队列,消费者监听绑定队列实现消息的接收和消费逻辑编写.简单模式下,强调的一个队列queue只被一个消 ...
- 多表连接的三种方式详解 hash join、merge join、 nested loop
在多表联合查询的时候,如果我们查看它的执行计划,就会发现里面有多表之间的连接方式.多表之间的连接有三种方式:Nested Loops,Hash Join 和 Sort Merge Join.具体适用哪 ...
- Spring4.X + spring MVC + Mybatis3 零配置应用开发框架搭建详解(1) - 基本介绍
Spring4.X + spring MVC + Mybatis3 零配置应用开发框架搭建详解(1) - 基本介绍 spring集成 mybatis Spring4.x零配置框架搭建 两年前一直在做后 ...
- [转]hibernate三种状态详解
本文来自 http://blog.sina.com.cn/u/2924525911 hibernate 三种状态详解 (2013-04-15 21:24:23) 转载▼ 分类: hibernate ...
- android中MVC,MVP和MVVM三种模式详解析
我们都知道,Android本身就采用了MVC模式,model层数据源层我们就不说了,至于view层即通过xml来体现,而 controller层的角色一般是由activity来担当的.虽然我们项目用到 ...
- Spring依赖注入三种方式详解
在讲解Spring依赖注入之前的准备工作: 下载包含Spring的工具jar包的压缩包 解压缩下载下来的Spring压缩包文件 解压缩之后我们会看到libs文件夹下有许多jar包,而我们只需要其中的c ...
随机推荐
- .NET 排序 Array.Sort<T> 实现分析
System.Array.Sort<T> 是.NET内置的排序方法, 灵活且高效, 大家都学过一些排序算法,比如冒泡排序,插入排序,堆排序等,不过你知道这个方法背后使用了什么排序算法吗? ...
- Java到底怎么学?
你现在是不是想学Java,但很迷茫不知该从何下手,那么请认真看完这篇文章,希望对你有所帮助! 作为零基础刚刚接触Java的朋友们来说,我的建议还是先看视频,虽然有很多人说看视频学习慢,建议直接买优秀的 ...
- 题解 [SBCOI2020] 一直在你身旁
题目传送门 题目大意 给出一个长度为 \(n\) 的单调不减的序列,每次可以选出一个点,产生贡献 \(a_k\),我们可以得知我们需要找到的数是否大于 \(k\).问找到要找到的数最小花费. \(n\ ...
- [对对子队]会议记录5.18(Scrum Meeting5)
今天已完成的工作 何瑞 工作内容:搭建第8关 相关issue:搭建关卡7.8.9 相关签入:feat:初步搭建了Lv8 吴昭邦 工作内容:搭建第8关 相关issue:搭建关卡7.8 ...
- logstash处理多行日志-处理java堆栈日志
logstash处理多行日志-处理java堆栈日志 一.背景 二.需求 三.实现思路 1.分析日志 2.实现,编写pipeline文件 四.注意事项 五.参考文档 一.背景 在我们的java程序中,经 ...
- LP-DDR 和其他 DDR
一篇技術文檔比較 LP-DDR 和其他 DDR. 就觀念來說,LP-DDR 就是 Low Power 的 DDR:但就架構來說,LP-DDR 和其他 DDR 是截然不同的東西. 他們分屬不同的 JDE ...
- c语言编程基础入门必备知识
数据类型 基本数据类型 类型名称说明char字符类型存放字符的ASCII码int整型存放有符号整数short短整型存放有符号整数long长整型存放有符号整数long long存放有符号整数float单 ...
- 关于把RTL工程代码封装成IP时对define宏定义参数的处理
在把RTL工程封装成IP的时候,如果工程中的代码中含有global include中定义的参数,则vivado不支持该参数文件的封装.出现IP_FLOW 19-4646的错误代码,解决方法: 1.在用 ...
- C++类的静态成员变量与静态成员函数
1.类的静态成员变量 C++类的静态成员变量主要有以下特性: 1.静态成员变量需要类内定义,类外初始化 2.静态成员变量不依赖于类,静态成员变量属于全局区,不属于类的空间. 3.静态成员变量通过类名访 ...
- 力扣 - 剑指 Offer 58 - I. 翻转单词顺序
题目 剑指 Offer 58 - I. 翻转单词顺序 思路1 假如题目要求我们翻转字符串,那么我们可以从末尾往前开始遍历每一个字符,同时将每一个字符添加到临时空间,最后输出临时空间的数据就完成翻转了, ...