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. centos7中 yum的安装

    自己误将yum卸载, 在重装时由于依赖问题一直报错: error: Failed dependencies:     /usr/bin/python is needed by yum-3.4.3-16 ...

  2. 10.3lambda表达式笔记

    与参数不同被捕获的变量的值是在lambda创建时拷贝,而不是调用时拷贝 void fcn() { int v1 = 42; //局部变量 auto f = [v1] { return v1; }; a ...

  3. 【Python】字符串的格式化

    一一对应  符号要用英文半角形式

  4. xshell如何将Windows文件上传到linux

    1.      首先先将你xshell配置好用户名及密码等,必须使用有权限下载的账号进行操作. 使用 yum provides */rz 这条命令,查看你系统自带的软件包的信息. 2.在输出的信息中可 ...

  5. WPF学习笔记三之绑定

    1.绑定模式 <TextBlock Margin="10" Text="LearningHard" Name="lbtext" Fon ...

  6. SpringBoot中的bean加载顺序

    https://www.dazhuanlan.com/2019/10/22/5daebc5d16429/ 最近在做传统Spring项目到SpringBoot项目迁移过程中,遇到了一些bean加载顺序的 ...

  7. SpringBoot整合WEB开发--(二)静态资源访问

    1.默认策略: 静态资源的位置一共5个,开发者可以将静态资源放到其中任意一个,分别是: "classpath:/META-INF/resources/", "classp ...

  8. 【荐】CSS实现漂亮实用带箭头的流程图

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  9. python面试的100题(10)

    18.反转一个整数,例如-123 --> -321 class Solution(object): def reverse(self,x): if -10<x<10: return ...

  10. 初识eclipse-java

    开始时会有工程的地址需要设置,最好将程序放在一个单独的文件夹中 有时候会用到外部的驱动程序,如excel等,就需要导入jar包 具体的请看下篇博客.