What’s Under the Hood of Spring AOP? A Deep Dive into Its Core Mechanisms
Spring AOP enables cross‑cutting concerns like logging and transactions without altering business code by leveraging dynamic proxies and method interceptor chains, and this article explains its core concepts, proxy selection (JDK vs CGLIB), call‑chain flow, key source classes, and provides a complete Java example.
Core Concepts of Spring AOP
Aspect : module that encapsulates cross‑cutting logic, marked with @Aspect.
JoinPoint : specific point during program execution, typically a method invocation.
Advice : action taken at a join point (e.g., @Before, @After, @Around).
Pointcut : predicate that matches join points, expressed with an AspectJ expression.
Proxy : object created by Spring that weaves the aspect logic around the target method.
Underlying Implementation Mechanisms
JDK Dynamic Proxy
Applicable when the target implements at least one interface.
Implementation: java.lang.reflect.Proxy creates a proxy that intercepts all interface method calls.
Advantages: uses only JDK classes, no third‑party dependency.
Limitations: cannot proxy concrete class methods directly.
CGLIB Dynamic Proxy
Applicable when the target does not implement any interface.
Implementation: CGLIB’s Enhancer generates a subclass of the target and overrides its methods to insert advice.
Advantages: can proxy ordinary class methods.
Limitations: slower object creation; final methods cannot be proxied.
Call‑Chain Analysis
Spring scans for classes annotated with @Aspect and registers them as aspect beans.
It matches the pointcut expression against candidate target methods.
For each matched target, Spring creates a proxy (JDK or CGLIB) and registers a chain of method interceptors.
When the client invokes a method on the proxy, the interceptor chain is triggered, executing in order:
Before advice
Around advice (pre‑processing)
Target method execution
After/AfterReturning/AfterThrowing advice
Client Call → Proxy Object → Method Interceptor Chain → Target Method ExecutionThe interceptor chain is built from implementations of org.aopalliance.intercept.MethodInterceptor. The core class that drives the chain is org.springframework.aop.framework.ReflectiveMethodInvocation, which iterates over the list of advisors (aspect + advice) and finally invokes the target method.
Complete Java Example
Maven Dependencies
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.3.26</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.3.26</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.9.22</version>
</dependency>Business Interface and Implementation
public interface UserService {
void addUser(String name);
}
public class UserServiceImpl implements UserService {
@Override
public void addUser(String name) {
System.out.println("新增用户: " + name);
}
}Aspect Definition
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
@Aspect
public class LogAspect {
@Before("execution(* UserService.addUser(..))")
public void beforeAddUser() {
System.out.println("日志切面: 即将新增用户");
}
}Spring Configuration
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.EnableAspectJAutoProxy;
@Configuration
@EnableAspectJAutoProxy
public class AppConfig {
@Bean
public UserService userService() {
return new UserServiceImpl();
}
@Bean
public LogAspect logAspect() {
return new LogAspect();
}
}Test Invocation
import org.springframework.context.ApplicationContext;
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
public class Main {
public static void main(String[] args) {
ApplicationContext context = new AnnotationConfigApplicationContext(AppConfig.class);
UserService userService = context.getBean(UserService.class);
userService.addUser("Shanisa");
}
}Running the program prints:
日志切面: 即将新增用户
新增用户: ShanisaKey Source Code Points
Proxy Creation
JDK proxy: Proxy.newProxyInstance() creates the proxy object.
CGLIB proxy: Enhancer creates a subclass of the target.
Method Interceptor Chain
Core class: org.springframework.aop.framework.ReflectiveMethodInvocation.
Invocation order: invoke() traverses the advisors list, invoking each MethodInterceptor before finally calling the target method.
Pointcut Matching
Implemented by AspectJExpressionPointcut, which evaluates the AspectJ expression to decide whether a method matches.
Conclusion
The essence of Spring AOP is the combination of a dynamic proxy (JDK or CGLIB) with a chain of method interceptors. Understanding this mechanism helps developers debug AOP configurations, optimise performance, and design clean cross‑cutting concerns.
java1234
Former senior programmer at a Fortune Global 500 company, dedicated to sharing Java expertise. Visit Feng's site: Java Knowledge Sharing, www.java1234.com
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.
