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 ...
随机推荐
- 导入org.apache.poi.xssf 读取excel
POI 操作 excel 用XSSF 方式时,如果不能自动导入 org.apache.poi.xssf 对应jar 包,则可以Apache 官网进行下载,自行导入. step1: 访问 http:/ ...
- go语言 内置的椭圆数字签名及其验证算法
package main import ( "crypto/ecdsa" "crypto/elliptic" "crypto/rand" & ...
- oracle 启动
Oracle数据库启动过程及状态详解(nomount.mount和open) 先来简要了解一下Oracle数据库体系架构以便于后面深入理解,Oracle Server主要由实例(instance)和数 ...
- Android 开发 微信分享,登陆,获取信息
1 获取appid和appsecret. https://open.weixin.qq.com/cgi-bin/index?t=home/index&lang=zh_CN ...
- 三分钟快速上手TensorFlow 2.0 (下)——模型的部署 、大规模训练、加速
前文:三分钟快速上手TensorFlow 2.0 (中)——常用模块和模型的部署 TensorFlow 模型导出 使用 SavedModel 完整导出模型 不仅包含参数的权值,还包含计算的流程(即计算 ...
- java基础之 控制语句
java基础之控制语句 在java中控制语句包括: if-else语句 swicth-case 语句 while循环 do-while循环 for循环 break语句 continue语句 其中:if ...
- Spark 中 GroupByKey 相对于 combineByKey, reduceByKey, foldByKey 的优缺点
避免使用GroupByKey 我们看一下两种计算word counts 的方法,一个使用reduceByKey,另一个使用 groupByKey: val words = Array("on ...
- bootstrap联动校验(转载)
接触bootstrapvalidator时间不久,最近需要多个字段共同验证,网上查了一下未找到,查阅api文档,发现确实可以实现. 先看dom <div class="form-gro ...
- oracle使用resultMap实现高级结果映射
resultMap的属性: 1.属性 id:resultMap的唯一标识.type:resulMap的映射结果类型(一般为Java实体类).2.子节点 id:一般对应数据库的主键 id,设置此项可以提 ...
- vue中style下scope的使用和坑
在vue组件中,为了使样式私有化(模块化),不对全局造成污染,可以在style标签上添加scoped属性以表示它的只属于当下的模块,这是一个非常好的举措,但是为什么要慎用呢?因为在我们需要修改公共组件 ...