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 编译器. ...
随机推荐
- VS2005 添加onTimer定时器
SetTimer(1,300,NULL); void CchangeDisplayDlg::OnTimer(UINT_PTR nIDEvent) { // TODO: 在此添加消息处理程序代码和/或调 ...
- directdraw显示rgb555
// TODO: 在此添加控件通知处理程序代码 height=width=widthBytes=0; m_screen.SetWindowPos(&CWnd::wndBottom,0,0, ...
- MySQL通过localhost无法连接数据库的解决
问题:一台服务器的PHP程序通过localhost地址无法连接数据库,但是如果设置为127.0.0.1则可以正常连接,连接其他数据库服务器也正常.MySQL的权限设置正确,且通过mysql命令行客户端 ...
- (七)java类和对象
一个类定义一个新的数据类型,也就是定义了一个逻辑框架,定义了它的成员之间的关系.可以通过这种类型来声明该类型的对象,通过new关键词来实例化对象,也就是为该类型的对象动态的分配物理内存空间,这个分配过 ...
- 完美的js运动框架
//完美运动框架, 对象,json,函数function move(obj,json,funEnd){clearInterval(obj.timer);//清除定时器obj.timer= setInt ...
- Django学习-12-模板继承
对于一下3个HTML页面 url(r'^templates1/', views.templates1), url(r'^templates2/', views.temp ...
- hdu5788 level up
贴下以前写的代码 比赛前我准备着重看的 主席树 树dp 字符串 #include<bits/stdc++.h> using namespace std; typedef long long ...
- ubuntu安装latex
1 终端中输入"sudo apt-get install texlive-full",输入root密码. 若不想安装所有文件,可以选择"sudo apt-get inst ...
- [SCOI2010]连续攻击游戏 匈牙利算法
觉得题目水的离开 不会匈牙利的请离开 不知道二分图的请离开 不屑的大佬请离开 ……. 感谢您贡献的访问量 ————————————华丽的分割线———————————— 扯淡完了,先重温一下题目 [SC ...
- React-Native安装使用
先附上React-Native官方文档中文版:http://wiki.jikexueyuan.com/project/react-native/getting-started.html 好,接下来我们 ...