热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

SpringAOP学习笔记Advice执行顺序

一、Advice执行顺序二、Advice在同一个Aspect中三、Advice在不同的Aspect中一、Advice执行顺序如果多个Advice和同一个JointPoint连接&

      • 一、Advice执行顺序
      • 二、Advice在同一个Aspect中
      • 三、Advice在不同的Aspect中


一、Advice执行顺序

如果多个Advice和同一个JointPoint连接,我们需要指明每一个Advice的执行顺序,那么我们就需要分两种情况讨论,

  • 第一种是如果Advice在同一个Aspect当中,那么执行的顺序是按照Advice在Aspect的声明顺序执行的。
  • 如果Advice在不同的Aspect当中,那么执行的顺序是需要将Aspect继承Ordered这个类,然后覆写getOrder方法

二、Advice在同一个Aspect中

AdviceOrder.java 在这个Aspect中我们指明了Pointcut和Advice的类型,我们可以看到,两个Before类型的Advice切入到了同一个JointPoint上,两个AfterReturning类型的Advice切入到了同一个JointPoint上。

package com.zbt.day04;import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;@Aspect
public class AdviceOrder {@Pointcut("execution(* com.zbt.day04.*.*(..))")public void methodPoint(){}@Before("methodPoint()")public void beforeAdviceI(){System.out.println("before advice I 执行了");}@Before("methodPoint()")public void beforeAdviceII(){System.out.println("before advice II 执行了");}@AfterReturning("methodPoint()")public void afterReturningI(){System.out.println("After Returning Advice I 执行了");}@AfterReturning("methodPoint()")public void afterReturningII(){System.out.println("After Returning Advice I 执行了");}
}

TargetClass.java 这是一个目标类,其中的sayHello方法是JointPoint

package com.zbt.day04;public class TargetClass {public void sayHello(){System.out.println("Hello,What a good day!");}
}

XML配置文档


<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xmlns:context&#61;"http://www.springframework.org/schema/context"xmlns:aop&#61;"http://www.springframework.org/schema/aop"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><aop:aspectj-autoproxy/><bean id&#61;"adviceOrder" class&#61;"com.zbt.day04.AdviceOrder">bean><bean id&#61;"targetClass" class&#61;"com.zbt.day04.TargetClass">bean>beans>

TestTargetClass.java 测试类

package com.zbt.day04;import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestAdvice {&#64;Testpublic void testTargetClass(){ApplicationContext context &#61; new ClassPathXmlApplicationContext("day04_config.xml");TargetClass targetClass &#61; context.getBean("targetClass",TargetClass.class);targetClass.sayHello();}
}

测试结果

before advice I 执行了
before advice II 执行了
Hello,What a good day!
After Returning Advice I 执行了
After Returning Advice II 执行了

从结果我们可以看到&#xff0c;这个beforeAdviceI在beforeAdviceII之前执行&#xff0c;而afterReturningI在afterReturningII之前执行了&#xff0c;正好印证了我们之前所说的在同一个Aspect内&#xff0c;那么如果多个相同类型的Advice连接到同一个JointPoint上&#xff0c;此时执行的顺序就是按照Advice在Aspect中声明的顺序执行的。

三、Advice在不同的Aspect中

如果有多个Aspect的相同类型的Advice切入同一个JointPoint当中&#xff0c;如果我们想要指定这写Advice执行的顺序&#xff0c;因此我们的Aspect需要继承org.springframework.core.Ordered这个类。
AdviceOrderI.java 这个类是一个Aspect继承了Order方法&#xff0c;同时覆写了getOrder方法。切入com.zbt.day05中所有的方法。提供了一个BeforeAdvice和一个AfterAdvice&#xff0c;同时

package com.zbt.day05;import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;&#64;Aspect
public class AdviceOrderI implements Ordered{&#64;Pointcut("execution(* com.zbt.day05.*.*(..))")public void pointI(){}&#64;Before("pointI()")public void beforeAdviceI(){System.out.println("AdviceOrderI 的 Before Advice I 执行了");}&#64;AfterReturning("pointI()")public void afterReturningI(){System.out.println("AdviceOrderI 的 After Returning Advice I 执行了");}&#64;Overridepublic int getOrder() {return 20;}
}

AdviceOrderII.java同样也继承了Ordered类&#xff0c;最后

package com.zbt.day05;import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.core.Ordered;&#64;Aspect
public class AdviceOrderII implements Ordered{&#64;Pointcut("com.zbt.day05.AdviceOrderI.pointI()")//复用Advicepublic void pointII(){}&#64;Before("pointII()")public void beforeAdviceII(){System.out.println("AdviceOrderII 的 Before Advice II 执行了");}&#64;AfterReturning("pointII()")public void afterReturningII(){System.out.println("AdviceOrderII 的 After Returning Advice II 执行了");}&#64;Overridepublic int getOrder() {return 10;}
}

TargetClass.java是一个目标类&#xff0c;sayHello是一个JointPoint

package com.zbt.day05;public class TargetClass {public void sayHello(){System.out.println("Hello,What a good day!");}
}

XML配置文档


<beans xmlns&#61;"http://www.springframework.org/schema/beans"xmlns:xsi&#61;"http://www.w3.org/2001/XMLSchema-instance"xmlns:context&#61;"http://www.springframework.org/schema/context"xmlns:aop&#61;"http://www.springframework.org/schema/aop"xsi:schemaLocation&#61;"http://www.springframework.org/schema/beanshttp://www.springframework.org/schema/beans/spring-beans.xsdhttp://www.springframework.org/schema/contexthttp://www.springframework.org/schema/context/spring-context.xsdhttp://www.springframework.org/schema/aophttp://www.springframework.org/schema/aop/spring-aop.xsd"><aop:aspectj-autoproxy/><bean id&#61;"adviceOrderI" class&#61;"com.zbt.day05.AdviceOrderI">bean><bean id&#61;"adviceOrderII" class&#61;"com.zbt.day05.AdviceOrderII">bean><bean id&#61;"targetClass" class&#61;"com.zbt.day05.TargetClass">bean>
beans>

TestTargetClass测试类

package com.zbt.day05;import com.zbt.day04.*;
import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;public class TestTargetClass {&#64;Testpublic void testTargetClass(){ApplicationContext context &#61; new ClassPathXmlApplicationContext("day05_config.xml");TargetClass targetClass &#61; context.getBean("targetClass", TargetClass.class);targetClass.sayHello();}
}

输出结果

AdviceOrderII 的 Before Advice II 执行了
AdviceOrderI 的 Before Advice I 执行了
Hello,What a good day!
AdviceOrderI 的 After Returning Advice I 执行了
AdviceOrderII 的 After Returning Advice II 执行了

从结果我们也可以看如果我们的Ordered返回的order越小&#xff0c;那么这个Aspect的BeforeAdvice就会在前面执行&#xff0c;而AfterReturning类型的Advice就会在后面执行。


推荐阅读
  • 将Web服务部署到Tomcat
    本文介绍了如何在JDeveloper 12c中创建一个Java项目,并将其打包为Web服务,然后部署到Tomcat服务器。内容涵盖从项目创建、编写Web服务代码、配置相关XML文件到最终的本地部署和验证。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文介绍了如何利用JavaScript或jQuery来判断网页中的文本框是否处于焦点状态,以及如何检测鼠标是否悬停在指定的HTML元素上。 ... [详细]
  • 前言--页数多了以后需要指定到某一页(只做了功能,样式没有细调)html ... [详细]
  • Python自动化处理:从Word文档提取内容并生成带水印的PDF
    本文介绍如何利用Python实现从特定网站下载Word文档,去除水印并添加自定义水印,最终将文档转换为PDF格式。该方法适用于批量处理和自动化需求。 ... [详细]
  • 在当前众多持久层框架中,MyBatis(前身为iBatis)凭借其轻量级、易用性和对SQL的直接支持,成为许多开发者的首选。本文将详细探讨MyBatis的核心概念、设计理念及其优势。 ... [详细]
  • Explore a common issue encountered when implementing an OAuth 1.0a API, specifically the inability to encode null objects and how to resolve it. ... [详细]
  • 技术分享:从动态网站提取站点密钥的解决方案
    本文探讨了如何从动态网站中提取站点密钥,特别是针对验证码(reCAPTCHA)的处理方法。通过结合Selenium和requests库,提供了详细的代码示例和优化建议。 ... [详细]
  • 1.如何在运行状态查看源代码?查看函数的源代码,我们通常会使用IDE来完成。比如在PyCharm中,你可以Ctrl+鼠标点击进入函数的源代码。那如果没有IDE呢?当我们想使用一个函 ... [详细]
  • 深入理解Cookie与Session会话管理
    本文详细介绍了如何通过HTTP响应和请求处理浏览器的Cookie信息,以及如何创建、设置和管理Cookie。同时探讨了会话跟踪技术中的Session机制,解释其原理及应用场景。 ... [详细]
  • 主要用了2个类来实现的,话不多说,直接看运行结果,然后在奉上源代码1.Index.javaimportjava.awt.Color;im ... [详细]
  • 深入理解Java中的volatile、内存屏障与CPU指令
    本文详细探讨了Java中volatile关键字的作用机制,以及其与内存屏障和CPU指令之间的关系。通过具体示例和专业解析,帮助读者更好地理解多线程编程中的同步问题。 ... [详细]
  • 本文介绍了如何使用JQuery实现省市二级联动和表单验证。首先,通过change事件监听用户选择的省份,并动态加载对应的城市列表。其次,详细讲解了使用Validation插件进行表单验证的方法,包括内置规则、自定义规则及实时验证功能。 ... [详细]
  • 本文详细介绍了如何使用Spring Boot进行高效开发,涵盖了配置、实例化容器以及核心注解的使用方法。 ... [详细]
  • 本文详细介绍了Akka中的BackoffSupervisor机制,探讨其在处理持久化失败和Actor重启时的应用。通过具体示例,展示了如何配置和使用BackoffSupervisor以实现更细粒度的异常处理。 ... [详细]
author-avatar
文之辅翼_770
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有