Spring AOP之通知类别
什么是通知类别
通知在切入点中执行的位置就是通知类别。关于通知和通知类别具体概念请移步我的另一篇博客Spring AOP之HelloWorld与概念介绍
5种通知类别介绍
在Spring AOP 中通知类别有5种分别是:
- before 前置通知
在方法执行前执行。如果方法出现异常也会执行。 - after 后置通知
后置通知在方法执行完毕后执行,如果方法出现异常也会执行。 - afterReturning 返回后通知
返回后通知是在方法正常返回后执行,如果方法出现异常不会执行 - afterThrowing 抛出异常后通知
抛出异常后通知是在方法抛出异常后执行,不抛异常不执行。 - around 环绕通知
环绕通知是在方法前后分别执行,可以控制方法的执行。
通知类别演示公共代码介绍
首先编写自定义通知类别 具体代码如下:
public class CustomAdvice {public void before(){System.out.println("before....");}public void after(){System.out.println("after....");} public void afterReturning(){System.out.println("afterReturning....");}public void afterThrowing(){System.out.println("afterThrowing....");} public void around(ProceedingJoinPoint joinPoint) throws Throwable {System.out.println("around before....");joinPoint.proceed();System.out.println("around after....");}
}
每个方法代表每个通知类别:
- before 方法 操作前置通知
- after 方法 操作后置通知
- afterReturning 方法 操作返回后通知
- afterThrowing 方法 操作抛出异常后通知
- around 方法 操作环绕通知
需要单独说明一下around 方法需要添加ProceedingJoinPoint 参数 并通过调用proceed方法表示执行切入的方法,具体效果请您耐心看下面的演示。
介绍完通知类下面就需要编写我们的目标类 具体代码如下:
public class User {public void run() {//int i = 1/0;System.out.println("run....");}
}
将通知类和目标类配置成Spring的Bean,具体配置如下:
<bean id="user" class="cn.zhuoqianmingyue.aop.adviceType.User"></bean>
<bean id="customAdvice" class="cn.zhuoqianmingyue.aop.adviceType.CustomAdvice"></bean>
1. before 前置通知 代码演示
before 前置通知 xml 配置文件中的配置如下:
<!-- aop 配置 --><aop:config><aop:pointcut expression="execution(* *..*.*(..))" id="customAdvicePointcut"/><aop:aspect ref="customAdvice"><aop:before method="before" pointcut-ref="customAdvicePointcut"/></aop:aspect></aop:config>
编写测试类 具体代码如下:
public class UserTest {@Testpublic void run() {ApplicationContext appliction = new ClassPathXmlApplicationContext("ioc-aop-adviceType.xml");User user = (User)appliction.getBean("user");user.run();}
}
测试结果:
2. after 后置通知 代码演示
after 后置通知 xml 配置文件中的配置如下:
<!-- aop 配置 --><aop:config><aop:pointcut expression="execution(* *..*.*(..))" id="customAdvicePointcut"/><aop:aspect ref="customAdvice"><aop:after method="after" pointcut-ref="customAdvicePointcut"/></aop:aspect></aop:config>
编写测试类 具体代码如下:
public class UserTest {@Testpublic void run() {ApplicationContext appliction = new ClassPathXmlApplicationContext("ioc-aop-adviceType.xml");User user = (User)appliction.getBean("user");user.run();}
}
测试结果:
3. afterReturning 返回后通知 代码演示
afterReturning 返回后通知 xml 配置文件中的配置如下:
<!-- aop 配置 --><aop:config><aop:pointcut expression="execution(* *..*.*(..))" id="customAdvicePointcut"/><aop:aspect ref="customAdvice"><aop:after-returning method="afterReturning" pointcut-ref="customAdvicePointcut"/></aop:aspect></aop:config>
编写测试类 具体代码如下:
public class UserTest {@Testpublic void run() {ApplicationContext appliction = new ClassPathXmlApplicationContext("ioc-aop-adviceType.xml");User user = (User)appliction.getBean("user");user.run();}
}
测试结果:
4. afterThrowing 抛出异常后通知 代码演示
afterThrowing 抛出异常后通知 xml 配置文件中的配置如下:
<!-- aop 配置 --><aop:config><aop:pointcut expression="execution(* *..*.*(..))" id="customAdvicePointcut"/><aop:aspect ref="customAdvice"><aop:after-throwing method="afterThrowing" pointcut-ref="customAdvicePointcut" /></aop:aspect></aop:config>
配置完成后我们需要将目标类中注释的一行代码放开如下图所示:
放开后就会报异常 只有报异常才会执行afterThrowing
编写测试类 具体代码如下:
public class UserTest {@Testpublic void run() {ApplicationContext appliction = new ClassPathXmlApplicationContext("ioc-aop-adviceType.xml");User user = (User)appliction.getBean("user");user.run();}
}
测试结果:
5. around 环绕通知 代码演示
around 环绕通知 xml 配置文件中的配置如下:
<!-- aop 配置 --><aop:config><aop:pointcut expression="execution(* *..*.*(..))" id="customAdvicePointcut"/><aop:aspect ref="customAdvice"><aop:around method="around" pointcut-ref="customAdvicePointcut"/> </aop:aspect></aop:config>
测试类具体代码如下:
public class UserTest {@Testpublic void run() {ApplicationContext appliction = new ClassPathXmlApplicationContext("ioc-aop-adviceType.xml");User user = (User)appliction.getBean("user");user.run();}
}
测试结果:
我们不放开目标类中的异常代码,也就是执行User类run方法不报错,5中通知都配置上进行测试(测试类配置和上面一直这里就不在进行粘贴了),结果如下:
5中通知都配置上进行测试的通知类中没有放开异常的代码,所以没有执行afterThrowing这个通知类别
我们放开目标类中的异常代码,也就是执行User类run方法报错5中通知都配置上进行测试(测试类配置和上面一直这里就不在进行粘贴了),结果如下:
如果出现异常 不会执行 afterReturning 和 around 的后置输出
查看源码
Spring AOP之通知类别相关推荐
- Spring AOP之通知类别执行顺序
以下内容来源:https://jinnianshilongnian.iteye.com/blog/1423489 如果我们有多个通知想要在同一连接点执行,那执行顺序如何确定呢?Spring AOP使用 ...
- spring aop 环绕通知around和其他通知的区别
前言: spring 的环绕通知和前置通知,后置通知有着很大的区别,主要有两个重要的区别: 1) 目标方法的调用由环绕通知决定,即你可以决定是否调用目标方法,而前置和后置通知 是不能决定的,他们只 ...
- Spring AOP(通知、连接点、切点、切面)
一.AOP术语 通知(Advice) 切面的工作被称为通知.通知定义了切面是什么以及何时使用.除了描述切面要完成的工作,通知还解决了何时执行这个工作的问题. 5种通知类型: 前置通知(Before ...
- Spring AOP 之 通知、连接点、切点、切面。
1:知识背景 软件系统可以看成是由一组关注点组成的,其中,直接的业务关注点,是直切关注点.而为直切关注点提供服务的,就是横切关注点. 2:面向切面的基本原理 什么是面向切面编程 横切关注点:影响应用多 ...
- Spring AOP 五大通知类型
1.前置通知 在目标方法执行之前执行执行的通知. 前置通知方法,可以没有参数,也可以额外接收一个JoinPoint,Spring会自动将该对象传入,代表当前的连接点,通过该对象可以获取目标对象 和 目 ...
- Spring AOP 前置通知
我们使用AspectJ对Spring进行AOP操作,有两种方式,注解和XML配置方式,先在pom.xml中声明jar包 <dependencies><dependency>&l ...
- spring aop环绕通知@Around
- springaop事务逻辑原理_搞懂Spring AOP,这一篇就够了
看了这篇文章,如果你还是不会用AOP来写程序,请你打我!! =.=||| 引言 Spring AOP是一个对AOP原理的一种实现方式,另外还有其他的AOP实现如AspectJ等. AOP意为面向切面编 ...
- SpringAop与AspectJ的联系与区别____比较分析 Spring AOP 和 AspectJ 之间的差别
SpringAop与AspectJ的联系与区别 区别 AspectJ AspectJ是一个面向切面的框架,它扩展了Java语言.AspectJ定义了AOP语法,所以它有一个专门的编译器用来生成遵守Ja ...
最新文章
- linux vi删除操作,vi删除操作
- 苏州宾馆管理也计算机哪个学校好,苏州十大寄宿式中学学校排名榜
- Pandas知识点-合并操作combine
- Linux基础(9)--延时与时间
- Spring Boot热部署
- js 解析php arraylist,使用JSON将ArrayList从Android发送到PHP脚本
- Using #region Directive With JavaScript Files in Visual Studio 【转载】
- 公众号openid能做用户识别_四川养老公众号开发哪里能做
- Latice CPLD jed转VME文件简介
- vscode 日志文件_vscode 日志文件_Visual Studio Code(VS code)介绍
- 传感器技术(徐科军 第四版)第四章:光电式传感器
- 由简入难学习3d机械制图软件顺序?Pro/Engineer 与CREO有何不同。
- coreseek拼音检索第三版性能测试
- 如何使用SPSS判断数据的正态分布
- 360主机卫士linux安装软件,360主机卫士Linux专杀版0.4.1 官方版(32位+64位)下载_云间下载...
- 什么是Numpy、Numpy教程
- vue移动端h5中a标签下载/预览文件
- 软件项目管理 3.2.预测生存期模型
- html和css学习课件(新版)
- Opencv4.0学习记录(Day21 视频文件摄像头使用)
热门文章
- 计算系统和计算机系统是同一个概念嘛,计算机操作系统的基本概念
- css3直线运动_CSS3中如何使元素曲线运动
- python更换国内源_一键更换国内云服务器Python pip镜像源至国内Pypi镜像源
- 在此iphone上尚未受信任_来自苹果这样的电话千万别接,不然 iPhone 没了...
- python快速运算符_Python基本运算符
- 生成介于0.95-1的随机数MATLAB,matlab生成随机数函数
- 故障转移集群仲裁盘_MongoDB负载均衡、故障转移及海量数据应对方案
- 边缘设备上的实时AI人员检测:在Raspberry Pi上启动SSD
- 使用JavaScript和jQuery添加工具提示
- mysql编译安装原理_MySQL编译安装全过程