spring AOP 采用和 AspectJ 一样的优先顺序来织入增强处理:在进入连接点时,高优先级的增强处理将先被织入;在退出连接点时,高优先级的增强处理会后被织入。

当不同的切面里的两个增强处理需要在同一个连接点被织入时,Spring AOP将以随机的顺序来织入这两个增强处理。如果应用需要指定不同切面类里增强处理的优先级,Spring提供了如下两种解决方案:

 让切面类实现org.springframework.core.Ordered接口,实现该接口只需实现一个int getOrder( )方法,该方法返回值越小,则优先级越高。

 直接使用@Order注解来修饰一个切面类,使用 @Order 时可指定一个int型的value属性,该属性值越小,则优先级越高。

Person.Java :

  1. public interface Person {
  2. public void eat(String food);
  3. }

Chinese.java :

  1. @Component
  2. public class Chinese implements Person {
  3. @Override
  4. public void eat(String food) {
  5. System.out.println("我正在吃:"+food);
  6. }
  7. }

AspectFirst.java :

  1. import org.aspectj.lang.annotation.After;
  2. import org.aspectj.lang.annotation.Aspect;
  3. import org.aspectj.lang.annotation.Before;
  4. import org.springframework.core.annotation.Order;
  5. @Aspect
  6. @Order(5)
  7. public class AspectFirst {
  8. @Before("execution(* com.bean.*.*(..))")
  9. public void aspectFirstStart(){
  10. System.out.println("@Before增强处理:我是AspectFirst切面,我的优先级为5");
  11. }
  12. @After("execution(* com.bean.*.*(..))")
  13. public void aspectFirstEnd(){
  14. System.out.println("@After增强处理:我是AspectFirst切面,我的优先级为5");
  15. }
  16. }

AspectSecond.java :

  1. import org.aspectj.lang.annotation.After;
  2. import org.aspectj.lang.annotation.Aspect;
  3. import org.aspectj.lang.annotation.Before;
  4. import org.springframework.core.annotation.Order;
  5. @Aspect
  6. @Order(1)
  7. public class AspectSecond {
  8. @Before("execution(* com.bean.*.*(..))")
  9. public void aspectSecondStart(){
  10. System.out.println("@Before增强处理:我是AspectSecond切面,我的优先级为1");
  11. }
  12. @After("execution(* com.bean.*.*(..))")
  13. public void aspectSecondEnd(){
  14. System.out.println("@After增强处理:我是AspectSecond切面,我的优先级为1");
  15. }
  16. }

bean.xml :

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xmlns:tx="http://www.springframework.org/schema/tx"
  7. xsi:schemaLocation="http://www.springframework.org/schema/beans
  8. http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
  9. http://www.springframework.org/schema/context
  10. http://www.springframework.org/schema/context/spring-context-2.5.xsd
  11. http://www.springframework.org/schema/tx
  12. http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
  13. http://www.springframework.org/schema/aop
  14. http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
  15. <context:component-scan base-package="com.bean">
  16. <context:include-filter type="annotation"
  17. expression="org.aspectj.lang.annotation.Aspect"/>
  18. </context:component-scan>
  19. <aop:aspectj-autoproxy/>
  20. </beans>

Test.java :

  1. public class Test {
  2. public static void main(String[] args) {
  3. ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
  4. Person p=(Person) ctx.getBean("chinese");
  5. p.eat("西瓜");
  6. }
  7. }

运行程序,控制台输出:

同一个切面类里的两个相同类型的增强处理在同一个连接点被织入时,Spring AOP将以随机顺序来织入这两个增强处理,没有办法指定它们的织入顺序。如果确实需要保证它们以固有的顺序被织入,则可考虑将多个增强处理压缩成一个,或者将不同增强处理重构到不同切面类中,通过在切面类级别上进行排序。

Spring AOP: 织入的顺序的更多相关文章

  1. Spring基础系列--AOP织入逻辑跟踪

    原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9619910.html 其实在之前的源码解读里面,关于织入的部分并没有说清楚,那些前置.后 ...

  2. Spring Aop织入点语法

    Aspectj织入点语法: 1.execution(public * *(..))   任何类的任何返回值的任何方法 2.execution(* set*(..))       任何类的set开头的方 ...

  3. Spring AOP之多切面运行顺序

    多切面运行顺序 当一个方法的执行被多个切面共同切的时候,环绕通知只影响当前切面的通知顺序,例如创建两个切面logUtil,validateUtil两个切面共同监视计算器类的加法运算,add(int a ...

  4. spring 切面织入报错:java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to...

    报这个错,只有一个原因,就是转化的类型不对. 接口过父类的子类,在强制转换的时候,一定要用接口父类来定义. 代码示例: package com.luoluo.dao.impl; import java ...

  5. Spring Aop之Cglib实现原理详解

    Spring Aop实现对目标对象的代理,AOP的两种实现方式:Jdk代理和Cglib代理.这两种代理的区别在于,Jdk代理与目标类都会实现同一个接口,并且在代理类中会调用目标类中被代理的方法,调用者 ...

  6. Spring Aop基于注解的实现

    一.AspectOriented Programing,面向切面编程.   AOP主要用于日志记录,性能统计,安全控制(权限控制),事务处理,异常处理等.将日志记录,性能统计,安全控制,事务处理,异常 ...

  7. Spring AOP全面详解(超级详细)

    如果说 IOC 是 Spring 的核心,那么面向切面编程AOP就是 Spring 另外一个最为重要的核心@mikechen AOP的定义 AOP (Aspect Orient Programming ...

  8. 框架源码系列三:手写Spring AOP(AOP分析、AOP概念学习、切面实现、织入实现)

    一.AOP分析 问题1:AOP是什么? Aspect Oriented Programming 面向切面编程,在不改变类的代码的情况下,对类方法进行功能增强. 问题2:我们需要做什么? 在我们的框架中 ...

  9. AOP的核心:代理与织入

    分为两步: 1.动态生成代理类: 2.织入: 2.6 织入(Weaving) 织入是将增强添加到目标的具体连接点上的过程 . AOP 织入方式: 方式 实现 应用编译期织入 特殊的 Java 编译器. ...

随机推荐

  1. shell脚本 awk工具

    awk工具概述awk编程语言/数据处理引擎基于模式匹配检查输入文本,逐行处理并输出通常在shell脚本中,或取指定的数据单独用时,可对文本数据做统计 命令格式格式一:awk [选项] '[条件]{编辑 ...

  2. 3.2 PCI设备的数据传递

    PCI设备的数据传递使用地址译码方式,当一个存储器读写总线事务到达PCI总线时,在这条总线上的所有PCI设备将进行地址译码,如果当前总线事务使用的地址在某个PCI设备的BAR空间中时,该PCI设备将使 ...

  3. PCI、CPCI、CPCIE 区别、特点

    PCI.CPCI.CPCIE 区别.特点 CPCI总线 •PCI总线作为处理器系统的局部总线,主要目的是为了连接外部设备,而不是作为处理器的系统总线连接Cache和主存储器 •(1) PCI总线空间与 ...

  4. FusionCharts封装-Value

    Data.java: /** * @Title:Data.java * @Package:com.fusionchart.model * @Description:FusionCharts 封装dat ...

  5. 查看dmp文件

    1.查看dmp文件,首先要通过以下的链接地址进行下载 http://msdl.microsoft.com/download/symbols/debuggers/dbg_x86_6.11.1.404.m ...

  6. java web面试题

    java web面试题 第1题.  编写一个Filter,需要() A.  继承Filter 类 B.  实现Filter 接口 C.  继承HttpFilter 类 D.  实现HttpFilter ...

  7. Linux显示2015年日历表

    Linux显示2015年日历表 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ cal 2015 2015 一月 二月 三月 日 一 二 三 四 五 六 日 一 ...

  8. Django学习-4-request获取数据

    app下views.py             获取前端HTML数据的一些方法             def func(request):                 # request.me ...

  9. Vasya and Basketball CodeForces - 493C

    Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows ...

  10. ASP.NET 使用Session,避免用户F5刷新时重复提交(转)

    1.使用Session,避免用户重复提交(F5刷新时) 0.起因         当用户上传文件后F5刷新浏览器会导致文件的重复提交和相关程序的重复执行.   1.实现原理         由于刷新提 ...