Spring AOP: 织入的顺序

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

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

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

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

Person.Java :

 

[java] view plain copy
 
  1. public interface Person {  
  2.     public void eat(String food);  
  3. }  

Chinese.java :

 

 

[java] view plain copy
 
  1. @Component  
  2. public class Chinese implements Person {  
  3.   
  4.     @Override  
  5.     public void eat(String food) {  
  6.         System.out.println("我正在吃:"+food);  
  7.     }  
  8.   
  9.       
  10. }  

AspectFirst.java :

 

 

[java] view plain copy
 
  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.   
  6. @Aspect  
  7. @Order(5)  
  8. public class AspectFirst {  
  9.       
  10.     @Before("execution(* com.bean.*.*(..))")  
  11.     public void aspectFirstStart(){  
  12.         System.out.println("@Before增强处理:我是AspectFirst切面,我的优先级为5");  
  13.     }  
  14.       
  15.     @After("execution(* com.bean.*.*(..))")  
  16.     public void aspectFirstEnd(){  
  17.         System.out.println("@After增强处理:我是AspectFirst切面,我的优先级为5");  
  18.     }  
  19. }  

AspectSecond.java :

 

 

[java] view plain copy
 
  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.   
  6. @Aspect  
  7. @Order(1)  
  8. public class AspectSecond {  
  9.       
  10.     @Before("execution(* com.bean.*.*(..))")  
  11.     public void aspectSecondStart(){  
  12.         System.out.println("@Before增强处理:我是AspectSecond切面,我的优先级为1");  
  13.     }  
  14.       
  15.     @After("execution(* com.bean.*.*(..))")  
  16.     public void aspectSecondEnd(){  
  17.         System.out.println("@After增强处理:我是AspectSecond切面,我的优先级为1");  
  18.     }  
  19. }  

bean.xml :

 

 

[html] view plain copy
 
  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.       
  16.       
  17.     <context:component-scan base-package="com.bean">  
  18.         <context:include-filter type="annotation"   
  19.                  expression="org.aspectj.lang.annotation.Aspect"/>  
  20.     </context:component-scan>  
  21.       
  22.     <aop:aspectj-autoproxy/>  
  23.       
  24.  </beans>  

Test.java :

 

 

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

运行程序,控制台输出:

 

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

posted on 2017-06-02 11:02  沐雨橙风丶  阅读(441)  评论(0编辑  收藏  举报