欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring-AOP @AspectJ进阶之增强织入的顺序

程序员文章站 2022-05-06 18:13:08
...

概述

一个连接点可以同时匹配多个切点,切点对应的增强在连接点上的织入顺序到底是如何安排呢?这个问题需要分三种情况讨论:

  • 如果增强在同一个切面类中声明,则依照增强在切面类中定义的顺序进行织入;

  • 如果增强位于不同的切面类中,且这些切面类都实现了org.springframework.core.Ordered接口,则由接口方法的顺序号决定(顺序号小的先织入);

  • 如果增强位于不同的切面类中,且这些切面类没有实现org.springframework.core.Ordered接口,织入的顺序是不确定的。

我们可以通过下图描述这种织入的规则:

Spring-AOP @AspectJ进阶之增强织入的顺序

切面类A和B都实现为Ordered接口,A切面类对应序号为1,B切面类对应序号为2,A切面类按顺序定义了3个增强,B切面类按顺序定义两个增强,这5个增强对应的切点都匹配某个目标类的连接点,则增强织入的顺序为图中虚线所示。


示例

代码已托管到Github—> https://github.com/yangshangwei/SpringMaster

Spring-AOP @AspectJ进阶之增强织入的顺序

业务类

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
相关标签: Ordered