说明:从网上百度后,可以知道,从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() 环绕后