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. linux HAProxy及Keepalived热备

    HAProxy 它是免费,快速且可靠的一种解决方案没,适用于那些负载特大的web站点这些站点通常又需要会话保持或七层处理提供高可用性,负载均衡及基于tcp和http应用的代理 衡量负载均衡器性能的因素 ...

  2. 【html5】html5本地简单存储

    html5本地简单存储 HTML5 提供了四种在客户端存储数据的新方法,即 localStorage .sessionStorage.globalStorage.Web Sql Database. 前 ...

  3. freemarker自定义标签报错(六)

    freemarker自定义标签 1.错误描述 freemarker.core.ParseException: Encountered "\"\u4f60\u597d\uff01\& ...

  4. 芝麻HTTP:如何寻找爬虫入口

    寻找爬虫入口 1 .本次任务的入口 这个爬虫的更好的入口就是我们平常使用的搜索引擎.搜索引擎虽然有很多种,但是其实都是在干一件事,收录网页,处理,然后提供搜索服务.在平时使用的过程中,我们通常都是直接 ...

  5. OpenStack_I版 3.glance部署

    存储镜像path                 默认镜像不存储在本地,一般放在swift对象存储或Cinder块存储里   glance安装     拷贝配置文件到/ect下,并新建配置目录,日志目 ...

  6. Linux系统 awk sed R脚本 python脚本传入变量

    sed 传入变量: chrI="chr2";sed -n "/$chrI/p" clippointpos.csv  #变量用$var表示,把sed的单引号变为双 ...

  7. Entity Framework Core必须牢记的三条引用三条命令

    关于EntityFramework Core有三个重要的引用和三条重要的命令,掌握以这六条,基本用Entity Framework Core就得心应手了. 引用1:Install-PackageMic ...

  8. tp5怎么实现搜索分页能保留搜索条件

    $profit=Db::view('profit','settlement_time,money,balance_account,balance_account1,did,user') ->vi ...

  9. Servlet程序

    编写: (1)搭建javaweb项目: 1,创建一个java项目:HelloServletWeb 2,在HelloServletWeb中创建一个文件夹webapp表示Web的根 3,在webapp中创 ...

  10. 【SPOJ】Distinct Substrings/New Distinct Substrings(后缀数组)

    [SPOJ]Distinct Substrings/New Distinct Substrings(后缀数组) 题面 Vjudge1 Vjudge2 题解 要求的是串的不同的子串个数 两道一模一样的题 ...