SpringAOP执行顺序的问题

说明:从网上百度后,可以知道,从Spring 5.2.7版本后,执行的顺序发生的改变,感兴趣的可以看下这篇文章你真的确定Spring AOP的执行顺序吗 https://zhuanlan.zhihu.com/p/266111498

一个切面的情况

在这里插入图片描述

两个切面的情况

在这里插入图片描述

环境

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.example</groupId>
    <artifactId>demo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>demo</name>
    <description>Demo project for Spring Boot</description>

    <properties>
        <java.version>1.8</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <spring-boot.version>2.3.7.RELEASE</spring-boot.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-aop</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
            <exclusions>
                <exclusion>
                    <groupId>org.junit.vintage</groupId>
                    <artifactId>junit-vintage-engine</artifactId>
                </exclusion>
            </exclusions>
        </dependency>
    </dependencies>

    <dependencyManagement>
        <dependencies>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-dependencies</artifactId>
                <version>${spring-boot.version}</version>
                <type>pom</type>
                <scope>import</scope>
            </dependency>
        </dependencies>
    </dependencyManagement>

    <build>
        <plugins>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-compiler-plugin</artifactId>
                <version>3.8.1</version>
                <configuration>
                    <source>1.8</source>
                    <target>1.8</target>
                    <encoding>UTF-8</encoding>
                </configuration>
            </plugin>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <version>2.3.7.RELEASE</version>
                <configuration>
                    <mainClass>com.example.demo.DemoApplication</mainClass>
                </configuration>
                <executions>
                    <execution>
                        <id>repackage</id>
                        <goals>
                            <goal>repackage</goal>
                        </goals>
                    </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

</project>

测试代码

package cn.tedu;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.*;
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
import org.springframework.core.annotation.Order;
import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;

/**
 * 本类用于
 *
 * @author SJXQ
 * @version 2021/12/1 10:18
 */
public class MyAopTest {

    public static void main(String[] args) {
        ApplicationContext context = new AnnotationConfigApplicationContext(MyConfig.class);
        UserService us = context.getBean(UserService.class);
        us.addUser();
    }

}
@Configuration
@ComponentScan("cn.tedu")
@EnableAspectJAutoProxy
class MyConfig{

}
interface UserService{
    void addUser();
}

@Service
class UserServiceImpl implements UserService{

    @Override
    public void addUser() {
        System.out.println("UserServiceImpl.addUser();");
    }
}

@Component
@Aspect
@Order(1)
class MyAop1 {
    @Pointcut("within(cn.tedu.UserServiceImpl)")
    public void pc() {
    }

    @Before("pc()")
    public void Before() {
        System.out.println("aop1.before() 前置通知");
    }

    @AfterReturning("pc()")
    public void afterReturning() {

        System.out.println("aop1.afterRetruning() 返回后通知");
    }

    @After("pc()")
    public void after() {
        System.out.println("aop1.after() 后置通知");
    }

    @AfterThrowing("pc()")
    public void afterThrowing() {
        System.out.println("aop1.afterThrowing() 抛异常后通知");
    }

    @Around("pc()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("aop1.around_before() 环绕前");
        Object result = joinPoint.proceed();
        System.out.println("aop1.around_after() 环绕后");
        return result;
    }
}

@Component
@Aspect
@Order(2)
class MyAop2 {
    @Pointcut("within(cn.tedu.UserServiceImpl)")
    public void pc() {
    }

    @Before("pc()")
    public void Before() {
        System.out.println("aop2.before() 前置通知");
    }

    @AfterReturning("pc()")
    public void afterReturning() {
        System.out.println("aop2.afterRetruning() 返回后通知");
    }

    @After("pc()")
    public void after() {
        System.out.println("aop2.after() 后置通知");
    }

    @AfterThrowing("pc()")
    public void afterThrowing() {
        System.out.println("aop2.afterThrowing() 抛异常后通知");
    }

    @Around("pc()")
    public Object around(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("aop2.around_before() 环绕前");
        Object result = joinPoint.proceed();
        System.out.println("aop2.around_after() 环绕后");
        return result;
    }
}

执行结果

aop1.around_before() 环绕前
aop1.before() 前置通知
aop2.around_before() 环绕前
aop2.before() 前置通知
UserServiceImpl.addUser();
aop2.afterRetruning() 返回后通知
aop2.after() 后置通知
aop2.around_after() 环绕后
aop1.afterRetruning() 返回后通知
aop1.after() 后置通知
aop1.around_after() 环绕后
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

水晶心泉

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值