热门标签 | 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就会在后面执行。


推荐阅读
  • 本文介绍如何使用布局文件在Android应用中排列多行TextView和Button,使其占据屏幕的特定比例,并提供示例代码以帮助理解和实现。 ... [详细]
  • RecyclerView初步学习(一)
    RecyclerView初步学习(一)ReCyclerView提供了一种插件式的编程模式,除了提供ViewHolder缓存模式,还可以自定义动画,分割符,布局样式,相比于传统的ListVi ... [详细]
  • Android 九宫格布局详解及实现:人人网应用示例
    本文深入探讨了人人网Android应用中独特的九宫格布局设计,解析其背后的GridView实现原理,并提供详细的代码示例。这种布局方式不仅美观大方,而且在现代Android应用中较为少见,值得开发者借鉴。 ... [详细]
  • DNN Community 和 Professional 版本的主要差异
    本文详细解析了 DotNetNuke (DNN) 的两种主要版本:Community 和 Professional。通过对比两者的功能和附加组件,帮助用户选择最适合其需求的版本。 ... [详细]
  • 将Web服务部署到Tomcat
    本文介绍了如何在JDeveloper 12c中创建一个Java项目,并将其打包为Web服务,然后部署到Tomcat服务器。内容涵盖从项目创建、编写Web服务代码、配置相关XML文件到最终的本地部署和验证。 ... [详细]
  • XNA 3.0 游戏编程:从 XML 文件加载数据
    本文介绍如何在 XNA 3.0 游戏项目中从 XML 文件加载数据。我们将探讨如何将 XML 数据序列化为二进制文件,并通过内容管道加载到游戏中。此外,还会涉及自定义类型读取器和写入器的实现。 ... [详细]
  • 本文详细介绍了 Java 中 org.apache.xmlbeans.SchemaType 类的 getBaseEnumType() 方法,提供了多个代码示例,并解释了其在不同场景下的使用方法。 ... [详细]
  • Scala 实现 UTF-8 编码属性文件读取与克隆
    本文介绍如何使用 Scala 以 UTF-8 编码方式读取属性文件,并实现属性文件的克隆功能。通过这种方式,可以确保配置文件在多线程环境下的一致性和高效性。 ... [详细]
  • 探讨如何真正掌握Java EE,包括所需技能、工具和实践经验。资深软件教学总监李刚分享了对毕业生简历中常见问题的看法,并提供了详尽的标准。 ... [详细]
  • 本文详细介绍了Java中org.neo4j.helpers.collection.Iterators.single()方法的功能、使用场景及代码示例,帮助开发者更好地理解和应用该方法。 ... [详细]
  • 优化ListView性能
    本文深入探讨了如何通过多种技术手段优化ListView的性能,包括视图复用、ViewHolder模式、分批加载数据、图片优化及内存管理等。这些方法能够显著提升应用的响应速度和用户体验。 ... [详细]
  • 本文深入探讨了 Java 中的 Serializable 接口,解释了其实现机制、用途及注意事项,帮助开发者更好地理解和使用序列化功能。 ... [详细]
  • UNP 第9章:主机名与地址转换
    本章探讨了用于在主机名和数值地址之间进行转换的函数,如gethostbyname和gethostbyaddr。此外,还介绍了getservbyname和getservbyport函数,用于在服务器名和端口号之间进行转换。 ... [详细]
  • 基于KVM的SRIOV直通配置及性能测试
    SRIOV介绍、VF直通配置,以及包转发率性能测试小慢哥的原创文章,欢迎转载目录?1.SRIOV介绍?2.环境说明?3.开启SRIOV?4.生成VF?5.VF ... [详细]
  • 解决JAX-WS动态客户端工厂弃用问题并迁移到XFire
    在处理Java项目中的JAR包冲突时,我们遇到了JaxWsDynamicClientFactory被弃用的问题,并成功将其迁移到org.codehaus.xfire.client。本文详细介绍了这一过程及解决方案。 ... [详细]
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社区 版权所有