spring 基于XML的申明式AspectJ通知的执行顺序
spring 基于XML的申明式AspectJ通知的执行顺序
关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关
1. XML文件配置说明
图片来源:《Java EE企业级应用开发教程》
2. 各种通知说明
前置通知
在执行方法之前执行
后置通知
在方法返回后执行
环绕通知
在方法前和后执行
异常通知
在方法抛出异常后执行
最终通知
在方法后执行
引介通知
略
注意后置通知和最终通知的区别:后置通知时在方法成功执行后会执行的,如果出现异常就不执行。而最终通知时无论是否出现异常都会执行的,感觉类似于finally
3. 在配置同一个切入点且不出现异常时的执行顺序
注意,椭圆中不区分顺序
4.具体顺序与配置文件的申明顺序有关
- 情况一
<aop:config><aop:aspect ref="myAspect"><aop:pointcut expression="execution(* springAspectJ.*.*(..))" id="myPointCut" /><aop:around method="myAround" pointcut-ref="myPointCut" /><aop:before method="myBefore" pointcut-ref="myPointCut" /><aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="returnVal"/><aop:after method="myAfter" pointcut-ref="myPointCut"/><aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/></aop:aspect></aop:config>
顺序:
环绕通知:前
前置通知
doSomething
环绕通知:后
后置通知(对应myAfterReturning)
最终通知
- 情况二
<aop:config><aop:aspect ref="myAspect"><aop:pointcut expression="execution(* springAspectJ.*.*(..))" id="myPointCut" /><aop:before method="myBefore" pointcut-ref="myPointCut" /><aop:around method="myAround" pointcut-ref="myPointCut" /><aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="returnVal"/><aop:after method="myAfter" pointcut-ref="myPointCut"/><aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/></aop:aspect></aop:config>
顺序:
前置通知
环绕通知:前
doSomething
环绕通知:后
后置通知
最终通知
结论一:前置通知和环绕通知的顺序和申明顺序有关,申明在前的先执行
- 情况三
当before在around前,后置和最终通知都在around后的时候
<aop:config><aop:aspect ref="myAspect"><aop:pointcut expression="execution(* springAspectJ.*.*(..))" id="myPointCut" /><aop:before method="myBefore" pointcut-ref="myPointCut" /><aop:around method="myAround" pointcut-ref="myPointCut" /><aop:after method="myAfter" pointcut-ref="myPointCut"/><aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="returnVal"/><aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/></aop:aspect></aop:config>
顺序
前置通知
环绕通知:前
doSomething
环绕通知:后
最终通知
后置通知
- 情况四
在三的前提条件下交换后置和最终的顺序,那么结果中的最终和后置的顺序也会交换
- 其他情况
当before和around的申明顺序变化时还会有不同以上的规律,这里就不一一列举的
总结
各种通知的执行顺序可能都不相同,情况有各种各样,但是只要配置的方法一样那么执行的顺序肯定是固定的
出错的方法的通知顺序也是和配置有关
以下是代码,供测试使用
相关依赖包下载
UserDao
public interface UserDao {String doSomething();
}
UserDaoImp
public class UserDaoImp implements UserDao{@Overridepublic void doSomething() {System.out.println("doSomething");}
}
MyAspect
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.JoinPoint;public class MyAspect {public void myBefore(JoinPoint joinpoint) {System.out.println("前置通知");}public void myAfterReturning(JoinPoint joinpoint, Object returnVal) {System.out.println("后置通知");}public Object myAround(ProceedingJoinPoint proceedingJoinPoint)throws Throwable {System.out.println("环绕通知:前");Object object = proceedingJoinPoint.proceed();System.out.println("环绕通知:后");return object;}public void myAfterThrowing(JoinPoint joinpoint, Throwable e) {System.out.println("异常:" + e.getMessage());}public void myAfter() {System.out.println("最终通知");}
}
Test
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class Test {public static void main(String[] args) {ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationAspectJ.xml");UserDao userDao = (UserDao) applicationContext.getBean("userDao");userDao.doSomething();}}
applicationAspectJ.xml
配置文件中的<aop:pointcut expression="execution(* springAspectJ.*.*(..))" id="myPointCut" />
中的springAspectJ改成你的包的路径
<?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:aop="http://www.springframework.org/schema/aop"xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.3.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-4.3.xsd"><bean id="userDao" class="springAspectJ.UserDaoImp"></bean><bean id="myAspect" class="springAspectJ.MyAspect"></bean><aop:config><aop:aspect ref="myAspect"><aop:pointcut expression="execution(* springAspectJ.*.*(..))" id="myPointCut" /><aop:before method="myBefore" pointcut-ref="myPointCut" /><aop:after-returning method="myAfterReturning" pointcut-ref="myPointCut" returning="returnVal"/><aop:after method="myAfter" pointcut-ref="myPointCut"/><aop:around method="myAround" pointcut-ref="myPointCut" /><aop:after-throwing method="myAfterThrowing" pointcut-ref="myPointCut" throwing="e"/></aop:aspect></aop:config>
</beans>
转载于:https://www.cnblogs.com/d-i-p/p/10676343.html
spring 基于XML的申明式AspectJ通知的执行顺序相关推荐
- spring基于XML的声明式事务控制-配置步骤
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 【spring】spring基于xml的声明式事务控制
结构 domain package com.itheima.domain;import java.io.Serializable;public class Account implements Ser ...
- Spring基于 XML 的声明式事务控制(配置方式)
一.引入依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http ...
- Spring基于XML装配Bean
Bean 的装配可以理解为依赖关系注入,Bean 的装配方式也就是 Bean 的依赖注入方式.Spring 容器支持多种形式的 Bean 的装配方式,如基于 XML 的 Bean 装配.基于 Anno ...
- Spring AOP之通知类别执行顺序
以下内容来源:https://jinnianshilongnian.iteye.com/blog/1423489 如果我们有多个通知想要在同一连接点执行,那执行顺序如何确定呢?Spring AOP使用 ...
- spring基于注解的声明式事务控制
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- spring基于XML的AOP-编写必要的代码
<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://mave ...
- 【Spring】spring基于注解的声明式事务控制
结构 domin package com.itheima.domain;import java.io.Serializable;public class Account implements Seri ...
- Spring基于xml自动装配
基于xml自动装配 手动装配 自动装配 首先写两个类Dept类和Emp类. Dept类: package com.Keafmd.spring5.autowire;/*** Keafmd** @Clas ...
最新文章
- Github高级搜索技巧
- 强化学习(十)Double DQN (DDQN)
- 从竞品数据搜集切入,NiucoData要做商业情报追踪分析工具
- javadoc 程序包android.content不存在,Eclipse中的Javadoc无法识别包
- 喜报|聚焦信创——360云计算管理平台生态建设的又一里程碑!
- 如何估算网站日承受最大访问PV
- iOS开发之将base64位编码的字符串转换为图片,data转图片,图片转data
- 摩拜单车微信小程序开发技术总结
- vue 后台翻译_vue - 实战项目 - 在线翻译
- 雪景特效制作软件JixiPix Snow Daze for Mac
- php工程师等级划分,PCB工程师的这四个等级,你都修炼到了什么级别?
- 软件代码数字签名基本原理
- hive中sql使用英文分号
- Java开发中存在这样的代码,反而影响整体整洁和可读性
- 消费者人群画像 python_如何正确打开相似人群画像算法
- 互联网日报 | 5月7日 星期五 | 街电与搜电完成合并;IBM发布2nm芯片制程;首届中国国际消费品博览会开幕...
- 香烟logo设计灵感 - logo设计公司 - ci设计
- 小猿圈python之python期末考试测试题(二)_小猿圈Python开发面试题(二)
- 设计 | 设计师常用的9大软件有哪些?看了顿时有方向了
- 常用的嵌入式硬件通信接口协议(UART、IIC、SPI、RS-232、RS-485、RS-422、CAN、USB、IRDA)(二)