Understanding Spring AOP Annotations and Their Execution Order
This article explains the common Spring AOP annotations such as @Before, @After, @AfterReturning, @AfterThrowing and @Around, demonstrates how to set up a Spring Boot project, write an interface, implementation, aspect, and discusses execution order, multiple aspects, and proxy‑failure scenarios.
Spring’s most powerful features are its IoC container and AOP support; this article walks through the most frequently used Spring AOP annotations and their execution order.
Common Spring AOP Annotations
@Before– executes before the target method. @After – executes after the target method (always runs). @AfterReturning – runs after successful completion (no exception). @AfterThrowing – runs when the target method throws an exception. @Around – surrounds the target method execution.
Typical Issues
1. How does Spring Boot 2 affect the order of AOP advice execution? 2. What common pitfalls appear when using AOP?
Example Code
Project Setup (Gradle)
plugins {
id 'org.springframework.boot' version '2.6.3'
id 'io.spring.dependency-management' version '1.0.11.RELEASE'
id 'java'
}
group 'io.zhengsh'
version '1.0-SNAPSHOT'
repositories {
mavenCentral()
maven { url 'https://repo.spring.io/milestone' }
maven { url 'https://repo.spring.io/snapshot' }
}
dependencies {
// basic web, actuator and AOP starters
implementation 'org.springframework.boot:spring-boot-starter-web'
implementation 'org.springframework.boot:spring-boot-starter-actuator'
implementation 'org.springframework.boot:spring-boot-starter-aop'
testImplementation 'org.springframework.boot:spring-boot-starter-test'
}
tasks.named('test') {
useJUnitPlatform()
}Service Interface
public interface CalcService {
public int div(int x, int y);
}Implementation Class
@Service
public class CalcServiceImpl implements CalcService {
@Override
public int div(int x, int y) {
int result = x / y;
System.out.println("====> CalcServiceImpl 被调用了,我们的计算结果是:" + result);
return result;
}
}Aspect Definition
@Aspect
@Component
public class MyAspect {
@Pointcut("execution(* io.zhengsh.spring.service.impl..*.*(..))")
public void divPointCut() {}
@Before("divPointCut()")
public void beforeNotify() {
System.out.println("----===>> @Before 我是前置通知");
}
@After("divPointCut")
public void afterNotify() {
System.out.println("----===>> @After 我是后置通知");
}
@AfterReturning("divPointCut")
public void afterReturningNotify() {
System.out.println("----===>> @AfterReturning 我是前置通知");
}
@AfterThrowing("divPointCut")
public void afterThrowingNotify() {
System.out.println("----===>> @AfterThrowing 我是异常通知");
}
@Around("divPointCut")
public Object around(ProceedingJoinPoint proceedingJoinPoint) throws Throwable {
System.out.println("----===>> @Around 环绕通知之前 AAA");
Object retVal = proceedingJoinPoint.proceed();
System.out.println("----===>> @Around 环绕通知之后 BBB");
return retVal;
}
}Proxy Failure Scenario
When a method in the same bean calls another method directly (e.g., this.b()), the second method bypasses the proxy and its advice is not applied.
@Service
public class AService {
public void a() {
System.out.println("...... a");
b(); // self‑invocation – AOP advice on b() will be skipped
}
public void b() {
System.out.println("...... b");
}
}Execution Conclusions
Tests on Spring 4.x / Spring Boot 1.5.9 showed that the @Around advice is the innermost execution. Later tests on Spring 5.3.15 / Spring Boot 2.6.3 confirmed the same behavior.
Multiple Aspects
When several aspects apply to the same join point, the @Order annotation determines precedence; a lower numeric value means higher priority.
Summary
The article provides a complete, runnable Spring AOP demo, explains annotation semantics, shows how to configure a Spring Boot project, and highlights common pitfalls such as self‑invocation and ordering of multiple aspects.
Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Top Architect
Top Architect focuses on sharing practical architecture knowledge, covering enterprise, system, website, large‑scale distributed, and high‑availability architectures, plus architecture adjustments using internet technologies. We welcome idea‑driven, sharing‑oriented architects to exchange and learn together.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
