Spring AOP: 织入的顺序
spring AOP 采用和 AspectJ 一样的优先顺序来织入增强处理:在进入连接点时,高优先级的增强处理将先被织入;在退出连接点时,高优先级的增强处理会后被织入。
当不同的切面里的两个增强处理需要在同一个连接点被织入时,Spring AOP将以随机的顺序来织入这两个增强处理。如果应用需要指定不同切面类里增强处理的优先级,Spring提供了如下两种解决方案:
① 让切面类实现org.springframework.core.Ordered接口,实现该接口只需实现一个int getOrder( )方法,该方法返回值越小,则优先级越高。
② 直接使用@Order注解来修饰一个切面类,使用 @Order 时可指定一个int型的value属性,该属性值越小,则优先级越高。
Person.Java :
- public interface Person {
- public void eat(String food);
- }
Chinese.java :
- @Component
- public class Chinese implements Person {
- @Override
- public void eat(String food) {
- System.out.println("我正在吃:"+food);
- }
- }
AspectFirst.java :
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.springframework.core.annotation.Order;
- @Aspect
- @Order(5)
- public class AspectFirst {
- @Before("execution(* com.bean.*.*(..))")
- public void aspectFirstStart(){
- System.out.println("@Before增强处理:我是AspectFirst切面,我的优先级为5");
- }
- @After("execution(* com.bean.*.*(..))")
- public void aspectFirstEnd(){
- System.out.println("@After增强处理:我是AspectFirst切面,我的优先级为5");
- }
- }
AspectSecond.java :
- import org.aspectj.lang.annotation.After;
- import org.aspectj.lang.annotation.Aspect;
- import org.aspectj.lang.annotation.Before;
- import org.springframework.core.annotation.Order;
- @Aspect
- @Order(1)
- public class AspectSecond {
- @Before("execution(* com.bean.*.*(..))")
- public void aspectSecondStart(){
- System.out.println("@Before增强处理:我是AspectSecond切面,我的优先级为1");
- }
- @After("execution(* com.bean.*.*(..))")
- public void aspectSecondEnd(){
- System.out.println("@After增强处理:我是AspectSecond切面,我的优先级为1");
- }
- }
bean.xml :
- <?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:context="http://www.springframework.org/schema/context"
- xmlns:aop="http://www.springframework.org/schema/aop"
- xmlns:tx="http://www.springframework.org/schema/tx"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-2.5.xsd
- http://www.springframework.org/schema/tx
- http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
- http://www.springframework.org/schema/aop
- http://www.springframework.org/schema/aop/spring-aop-2.5.xsd">
- <context:component-scan base-package="com.bean">
- <context:include-filter type="annotation"
- expression="org.aspectj.lang.annotation.Aspect"/>
- </context:component-scan>
- <aop:aspectj-autoproxy/>
- </beans>
Test.java :
- public class Test {
- public static void main(String[] args) {
- ApplicationContext ctx=new ClassPathXmlApplicationContext("bean.xml");
- Person p=(Person) ctx.getBean("chinese");
- p.eat("西瓜");
- }
- }
运行程序,控制台输出:
同一个切面类里的两个相同类型的增强处理在同一个连接点被织入时,Spring AOP将以随机顺序来织入这两个增强处理,没有办法指定它们的织入顺序。如果确实需要保证它们以固有的顺序被织入,则可考虑将多个增强处理压缩成一个,或者将不同增强处理重构到不同切面类中,通过在切面类级别上进行排序。
Spring AOP: 织入的顺序的更多相关文章
- Spring基础系列--AOP织入逻辑跟踪
原创作品,可以转载,但是请标注出处地址:https://www.cnblogs.com/V1haoge/p/9619910.html 其实在之前的源码解读里面,关于织入的部分并没有说清楚,那些前置.后 ...
- Spring Aop织入点语法
Aspectj织入点语法: 1.execution(public * *(..)) 任何类的任何返回值的任何方法 2.execution(* set*(..)) 任何类的set开头的方 ...
- Spring AOP之多切面运行顺序
多切面运行顺序 当一个方法的执行被多个切面共同切的时候,环绕通知只影响当前切面的通知顺序,例如创建两个切面logUtil,validateUtil两个切面共同监视计算器类的加法运算,add(int a ...
- spring 切面织入报错:java.lang.ClassCastException: com.sun.proxy.$Proxy7 cannot be cast to...
报这个错,只有一个原因,就是转化的类型不对. 接口过父类的子类,在强制转换的时候,一定要用接口父类来定义. 代码示例: package com.luoluo.dao.impl; import java ...
- Spring Aop之Cglib实现原理详解
Spring Aop实现对目标对象的代理,AOP的两种实现方式:Jdk代理和Cglib代理.这两种代理的区别在于,Jdk代理与目标类都会实现同一个接口,并且在代理类中会调用目标类中被代理的方法,调用者 ...
- Spring Aop基于注解的实现
一.AspectOriented Programing,面向切面编程. AOP主要用于日志记录,性能统计,安全控制(权限控制),事务处理,异常处理等.将日志记录,性能统计,安全控制,事务处理,异常 ...
- Spring AOP全面详解(超级详细)
如果说 IOC 是 Spring 的核心,那么面向切面编程AOP就是 Spring 另外一个最为重要的核心@mikechen AOP的定义 AOP (Aspect Orient Programming ...
- 框架源码系列三:手写Spring AOP(AOP分析、AOP概念学习、切面实现、织入实现)
一.AOP分析 问题1:AOP是什么? Aspect Oriented Programming 面向切面编程,在不改变类的代码的情况下,对类方法进行功能增强. 问题2:我们需要做什么? 在我们的框架中 ...
- AOP的核心:代理与织入
分为两步: 1.动态生成代理类: 2.织入: 2.6 织入(Weaving) 织入是将增强添加到目标的具体连接点上的过程 . AOP 织入方式: 方式 实现 应用编译期织入 特殊的 Java 编译器. ...
随机推荐
- shell脚本 awk工具
awk工具概述awk编程语言/数据处理引擎基于模式匹配检查输入文本,逐行处理并输出通常在shell脚本中,或取指定的数据单独用时,可对文本数据做统计 命令格式格式一:awk [选项] '[条件]{编辑 ...
- 3.2 PCI设备的数据传递
PCI设备的数据传递使用地址译码方式,当一个存储器读写总线事务到达PCI总线时,在这条总线上的所有PCI设备将进行地址译码,如果当前总线事务使用的地址在某个PCI设备的BAR空间中时,该PCI设备将使 ...
- PCI、CPCI、CPCIE 区别、特点
PCI.CPCI.CPCIE 区别.特点 CPCI总线 •PCI总线作为处理器系统的局部总线,主要目的是为了连接外部设备,而不是作为处理器的系统总线连接Cache和主存储器 •(1) PCI总线空间与 ...
- FusionCharts封装-Value
Data.java: /** * @Title:Data.java * @Package:com.fusionchart.model * @Description:FusionCharts 封装dat ...
- 查看dmp文件
1.查看dmp文件,首先要通过以下的链接地址进行下载 http://msdl.microsoft.com/download/symbols/debuggers/dbg_x86_6.11.1.404.m ...
- java web面试题
java web面试题 第1题. 编写一个Filter,需要() A. 继承Filter 类 B. 实现Filter 接口 C. 继承HttpFilter 类 D. 实现HttpFilter ...
- Linux显示2015年日历表
Linux显示2015年日历表 youhaidong@youhaidong-ThinkPad-Edge-E545:~$ cal 2015 2015 一月 二月 三月 日 一 二 三 四 五 六 日 一 ...
- Django学习-4-request获取数据
app下views.py 获取前端HTML数据的一些方法 def func(request): # request.me ...
- Vasya and Basketball CodeForces - 493C
Vasya follows a basketball game and marks the distances from which each team makes a throw. He knows ...
- ASP.NET 使用Session,避免用户F5刷新时重复提交(转)
1.使用Session,避免用户重复提交(F5刷新时) 0.起因 当用户上传文件后F5刷新浏览器会导致文件的重复提交和相关程序的重复执行. 1.实现原理 由于刷新提 ...