1. 程式人生 > >Spring-AOP @AspectJ進階之增強織入的順序

Spring-AOP @AspectJ進階之增強織入的順序

概述

一個連線點可以同時匹配多個切點,切點對應的增強在連線點上的織入順序到底是如何安排呢?這個問題需要分三種情況討論:

  • 如果增強在同一個切面類中宣告,則依照增強在切面類中定義的順序進行織入;

  • 如果增強位於不同的切面類中,且這些切面類都實現了org.springframework.core.Ordered介面,則由介面方法的順序號決定(順序號小的先織入);

  • 如果增強位於不同的切面類中,且這些切面類沒有實現org.springframework.core.Ordered介面,織入的順序是不確定的。

我們可以通過下圖描述這種織入的規則:

這裡寫圖片描述

切面類A和B都實現為Ordered介面,A切面類對應序號為1,B切面類對應序號為2,A切面類按順序定義了3個增強,B切面類按順序定義兩個增強,這5個增強對應的切點都匹配某個目標類的連線點,則增強織入的順序為圖中虛線所示。

示例

這裡寫圖片描述

業務類

package com.xgj.aop.spring.advisor.aspectJAdvance.pointOrder;

import org.springframework.stereotype.Component;

/**
 * 
 * 
 * @ClassName: Bussiness
 * 
 * @Description: @Component標註的Bean
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年9月11日 上午1:38:06
 */

@Component
public class Bussiness {
public void dealBussiness() { System.out.println("dealBussiness executed"); } }

切面1

package com.xgj.aop.spring.advisor.aspectJAdvance.pointOrder;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;

/**
 * 
 * 
 * @ClassName
: PointOrderOneAspect * * @Description: @Aspect標註的切面,實現Ordered介面 重寫getOrder方法 後者 使用註解 @Order(value = 1) * * @author: Mr.Yang * * @date: 2017年9月11日 上午1:35:42 */
@Aspect @Order(value = 1) public class PointOrderOneAspect { @Before("execution(* dealBussiness(..))") public void method1() { System.out.println("method1 executed"); } @Before("execution(* dealBussiness(..))") public void method2() { System.out.println("method2 executed"); } @Before("execution(* dealBussiness(..))") public void method3() { System.out.println("method3 executed"); } }

切面2

package com.xgj.aop.spring.advisor.aspectJAdvance.pointOrder;

import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.core.annotation.Order;

/**
 * 
 * 
 * @ClassName: PointOrderTwoAspect
 * 
 * @Description: @Aspect標註的切面,實現Ordered介面 重寫getOrder方法 後者 使用註解 @Order(value = 2)
 * 
 * @author: Mr.Yang
 * 
 * @date: 2017年9月11日 上午1:36:02
 */

@Aspect
@Order(value = 2)
public class PointOrderTwoAspect {

    @Before("execution(* dealBussiness(..))")
    public void method4() {
        System.out.println("method4 executed");
    }

    @Before("execution(* dealBussiness(..))")
    public void method5() {
        System.out.println("method5 executed");
    }

}

配置檔案

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:aop="http://www.springframework.org/schema/aop"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/aop
        http://www.springframework.org/schema/aop/spring-aop.xsd
        http://www.springframework.org/schema/context 
        http://www.springframework.org/schema/context/spring-context.xsd">

<!-- (1)宣告Context名稱空間以及Schema檔案   (2)掃描類包以及應用註解定義的bean -->
<context:component-scan base-package="com.xgj.aop.spring.advisor.aspectJAdvance.pointOrder"/>

<!-- 基於@AspectJ切面的驅動器 -->
<aop:aspectj-autoproxy proxy-target-class="true"/>

<!-- 使用了@AspectJ註解的切面類 -->
<bean class="com.xgj.aop.spring.advisor.aspectJAdvance.pointOrder.PointOrderOneAspect"/>
<bean class="com.xgj.aop.spring.advisor.aspectJAdvance.pointOrder.PointOrderTwoAspect"/>

</beans>

測試類

package com.xgj.aop.spring.advisor.aspectJAdvance.pointOrder;

import org.junit.Test;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class PointOrderAspectTest {

    @Test
    public void test() {
        ApplicationContext ctx = new ClassPathXmlApplicationContext(
                "com/xgj/aop/spring/advisor/aspectJAdvance/pointOrder/conf-pointOrder.xml");

        Bussiness bussiness = ctx.getBean("bussiness", Bussiness.class);

        bussiness.dealBussiness();
    }
}

執行結果

2017-09-11 01:46:32,790  INFO [main] (AbstractApplicationContext.java:583) - Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@3695de1a: startup date [Mon Sep 11 01:46:32 BOT 2017]; root of context hierarchy
2017-09-11 01:46:32,869  INFO [main] (XmlBeanDefinitionReader.java:317) - Loading XML bean definitions from class path resource [com/xgj/aop/spring/advisor/aspectJAdvance/pointOrder/conf-pointOrder.xml]
method1 executed
method2 executed
method3 executed
method4 executed
method5 executed
dealBussiness executed