spring 基于XML的申明式AspectJ通知的执行顺序

关于各种通知的执行顺序,结论:与配置文件中的申明顺序有关

1. XML文件配置说明

图片来源:《Java EE企业级应用开发教程》

2. 各种通知说明

  1. 前置通知

在执行方法之前执行

  1. 后置通知

在方法返回后执行

  1. 环绕通知

在方法前和后执行

  1. 异常通知

在方法抛出异常后执行

  1. 最终通知

在方法后执行

  1. 引介通知

注意后置通知和最终通知的区别:后置通知时在方法成功执行后会执行的,如果出现异常就不执行。而最终通知时无论是否出现异常都会执行的,感觉类似于finally

3. 在配置同一个切入点且不出现异常时的执行顺序

注意,椭圆中不区分顺序

4.具体顺序与配置文件的申明顺序有关

  1. 情况一
    <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)

最终通知

  1. 情况二
	<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

环绕通知:后

后置通知

最终通知

结论一:前置通知和环绕通知的顺序和申明顺序有关,申明在前的先执行

  1. 情况三

当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

环绕通知:后

最终通知

后置通知

  1. 情况四

在三的前提条件下交换后置和最终的顺序,那么结果中的最终和后置的顺序也会交换

  1. 其他情况

当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通知的执行顺序的更多相关文章

  1. spring 基于xml的申明式AspectH中的后置通知的返回值获取

    spring 基于xml的申明式AspectH中的后置通知的返回值获取 1. 配置文件 <aop:config> <aop:aspect ref="myAspect&quo ...

  2. spring基于xml的声明式事务控制配置步骤

    <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.sp ...

  3. spring基于XML的声明式事务控制

    <?xml version="1.0" encoding="utf-8" ?><beans xmlns="http://www.sp ...

  4. 阶段3 2.Spring_10.Spring中事务控制_6 spring基于XML的声明式事务控制-配置步骤

    环境搭建 新建工程 把对应的依赖复制过来 src下内容复制 配置spring中的声明事物 找到bean.xml开始配置 配置事物管理器 里面需要注入DataSource 2-配置事物通知 需要先导入事 ...

  5. 吴裕雄--天生自然JAVA SPRING框架开发学习笔记:Spring基于XML装配Bean

    Bean 的装配可以理解为依赖关系注入,Bean 的装配方式也就是 Bean 的依赖注入方式.Spring 容器支持多种形式的 Bean 的装配方式,如基于 XML 的 Bean 装配.基于 Anno ...

  6. Spring--AOP、通知的执行顺序

    AOP执行顺序 如果我们在同一个方法自定义多个AOP,我们如何指定他们的执行顺序呢? 可以通过指定order,order越小越是最先执行. 配置AOP执行顺序的三种方式: 通过实现Ordered接口 ...

  7. SSM框架—Spring AOP之基于注解的声明式AspectJ(Demo)

    项目结构 XML <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http ...

  8. spring基于xml的事务控制

    opm配置 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="http: ...

  9. 基于XML的声明式事务控制

    1.maven依赖 <?xml version="1.0" encoding="UTF-8"?> <project xmlns="h ...

随机推荐

  1. Java 中的 匿名类

    什么是内部类? 在一个类中定义另一个类,这样定义的类称为内部类.包含内部类的类称为内部类的外部类. 如果想要通过一个类来使用另一个类,可以定义为内部类. 内部类的外部类的成员变量在内部类仍然有效,内部 ...

  2. K3/Cloud点按钮打开单据,列表,动态表单,简单账表和直接Sql报表示例

    BOS IDE中配置了个界面,拖了动态表单界面,加了5个测试按钮. 点击“打开单据”维护界面, 会跳转到一个新的主界面页签,[物料]新增 点击“打开列表”,会弹出[物料]列表界面 点击“打开动态表单” ...

  3. adb 连接手机

    adb kill-server adb start-server 可能会遇到问题华为手机: 有应用遮挡了权限请求界面,设置应用无法验证你的回应 系统导航关闭悬浮球 然后重启adb  server ad ...

  4. AcWing 154. 滑动窗口

    https://www.acwing.com/problem/content/156/ #include <iostream> using namespace std; ; int a[N ...

  5. 10day 系统的selinux服务程序优化

    selinux服务对root用户权限进行控制 很多企业中:selinux服务默认关闭 centos6==centos7 临时关闭: 检查确认: getenforce --- 确认selinux服务是否 ...

  6. 在手机浏览器中判断App是否已安装

    从网上搜到之前手机中判断App是否安装可以通过onblur事件+定时器来实现. 但现在要做这个功能时,按网上的说法已经不能实现了.因为现在浏览器中打开App,window不会触发onblur事件. 在 ...

  7. Js选择器总结

    一.原生JS选择器 JS选择器常用的有getElementById().getElementsByName().getElementsByTagName().getElementsByClassNam ...

  8. AJAX-状态属性

    XMLHttpRequest对象的readyState属性 作用:表示xhr对象的请求状态 值:由0到4表示5个状态 0:请求尚未初始化 1:已经打开到WEB服务器的连接,正在向服务器发送请求 2:请 ...

  9. jmeter+influxdb+granfana+collectd监控cpu+mem+TPS

    1.安装grafana #####gafana过期安装包安装报错 Error unpacking rpm package grafana-5.1.4-1.x86_64error: unpacking ...

  10. css3之渐变背景色(linear-gradient)

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...