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{
@Override
public 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>
spring 基于XML的申明式AspectJ通知的执行顺序的更多相关文章
- spring 基于xml的申明式AspectH中的后置通知的返回值获取
spring 基于xml的申明式AspectH中的后置通知的返回值获取 1. 配置文件 <aop:config> <aop:aspect ref="myAspect&quo ...
- spring基于xml的声明式事务控制配置步骤
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...
- spring基于XML的声明式事务控制
<?xml version="1.0" encoding="utf-8" ?><beans xmlns="http://www.sp ...
- 阶段3 2.Spring_10.Spring中事务控制_6 spring基于XML的声明式事务控制-配置步骤
环境搭建 新建工程 把对应的依赖复制过来 src下内容复制 配置spring中的声明事物 找到bean.xml开始配置 配置事物管理器 里面需要注入DataSource 2-配置事物通知 需要先导入事 ...
- 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于XML装配Bean
Bean 的装配可以理解为依赖关系注入,Bean 的装配方式也就是 Bean 的依赖注入方式.Spring 容器支持多种形式的 Bean 的装配方式,如基于 XML 的 Bean 装配.基于 Anno ...
- Spring--AOP、通知的执行顺序
AOP执行顺序 如果我们在同一个方法自定义多个AOP,我们如何指定他们的执行顺序呢? 可以通过指定order,order越小越是最先执行. 配置AOP执行顺序的三种方式: 通过实现Ordered接口 ...
- SSM框架—Spring AOP之基于注解的声明式AspectJ(Demo)
项目结构 XML <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...
- spring基于xml的事务控制
opm配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...
- 基于XML的声明式事务控制
1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...
随机推荐
- 棋盘划分问题中4的k次方减一是三的倍数
1.数学归纳法(万物皆可数学归纳) ①当n=1时:4-1=3(是三的倍数) ②假设n-1成立证明n成立:4n-1=4n-1*(4-1)+4n-1-1 =3*4n-1+(4n-1-1) 所以4n-1%3 ...
- go语言 RSA数字签名和验证签名
package main import ( "crypto" "crypto/rand" "crypto/rsa" "crypto ...
- Visual detection of structural changes in time-varying graphs using persistent homology
PKU blog about this paper Basic knowledge: 1. what is time-varying graphs? time-varying graph VS st ...
- c#窗体进度条
c#窗体进度条 //进度条的最大值 ; progressBar1.Maximum = Convert.ToInt32(a); ;i<progressBar1.Maximum;i++) { //进 ...
- Scale9Sprite 的 setCapInsets中需要注意的地方
在设置 setCapInsets()方法的y参数的时候,不直接取cocosStudio中的y,而是取Scale9Sprite.height - cocosStudio中的y.
- find & grep 总 结
前言 关于本文 总 结 了 find.grep常 规 用 法,正 则 表 达 式,find与 grep合 用 以 及 自 定 义 搜 索 函 数 等 什么是find和grep find 和 grep ...
- [C#] 委托与匿名方法
using System; namespace 匿名函数 { class Program { delegate void TestDelegate(string s); static void M(s ...
- nginx mime.types 常用配置
常见mime type类型 nginx的mime.types 配置如下 types { text/html html htm shtml; text/css css; text/xml xml pli ...
- 每天进步一点点------Allegro 手工布线时控制面板各选项说明
在进行手工布线过程中,最重要的就是对控制面板中的各个选项进行设置,因此首先介绍控制面板中各个选项的含义. 手工布线的命令为Route->connect,执行命令后,右侧控制面板如图8.14所示. ...
- pyodbc-的一些说明
cursor的description 可以获得一些关于表的信息 info=cursor.description 此时表中有多少列就有多少个元素,一个元素就是一列的信息(格式是tuple),所以这里的i ...