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 ...
随机推荐
- 杭电oj1717——小数化分数(java实现)
question:小数化分数2 思路: /** * 这道题没有整数部分(有也无所谓,算小数部分,算完了分子分母按倍数加上就好),也就是说数组直接从a[2]开始后面是小数,我把这道题分为了三类: * * ...
- 服务器上搭建使用SSH账户登录的Git仓库
1.安装git yum install -y git 2.创建git仓库保存的目录 mkdir /data/git_repo 3.初始化空仓库 cd /data/git_repogit init -- ...
- EF中的持久化场景
使用EF实现实体持久化(保存)到数据库有两种情况:在线场景和离线场景. 1.在线场景 在线场景中,context是上下文实例,读写都通过一个context. 这种方案适用于连接本地数据库或同一网络上的 ...
- 解决pycharm创建github工程但push失败的问题
1.运行git-cmd.exe; 2.d: cd 工程路径 3.执行命令:git remote set-url origin https://github.com/wawj901124/jmet ...
- PP: UMAP: uniform manifold approximation and projection for dimension reduction
From Tutte institute for mathematics and computing Problem: dimension reduction Theoretical foundati ...
- 在多租户(容器)数据库中如何创建PDB:方法1 从种子创建PDB
基于版本:19c (12.2.0.3) AskScuti 创建方法:从零开始创建一个PDB(从PDB$SEED创建新的PDB) 对应路径:Creating a PDB --> Creating ...
- java基础(十三)之接口
接口 什么是接口? 生活中也有很多的接口,比如USB接口.定义了接口就是定义了调用对象的标准. 接口基本语法 1.使用interface定义:2.接口当中的方法都是抽象方法:因为抽象函数不能生成对象, ...
- 正则表达式过滤url请求
过滤url中带reset的url请求 atgBusSignFilter.setSignUriRegex("^.*/reset/.*$")等价于 atgBusSignFilter.s ...
- vs2019 scanf 解决 C4996问题
1. 首先选择项目 2. 然后选择最下面那行的 工程属性, 其后于此处 3. 添加上 :_CRT_SECURE_NO_WARNINGS 最后保存,使用 scanf 读取即无报错了
- F与Q查询
F查询: 之前构造的过滤器都是将字段值与某个我们设定的常亮做比较,如果我们要对两个字段的字段的值做比较久需要用到F查询:F查询可以用来比较同一个model事例中两个不同字段的值, 准备工作: 创建数据 ...