Spring AOP Internals: Join Points, Advice, and Weaving Explained
This article explains Spring AOP's core concepts including join points, advice abstraction, and weaving mechanisms, detailing the AOP Alliance interface design with JoinPoint and MethodInterceptor, and contrasting static versus dynamic weaving approaches used by Spring.
Learning a module's design starts with its interface design; through interfaces we understand the overall implementation, and concrete implementation becomes assembling these interfaces.
AOP Background
Object-oriented programming (OOP) has drawbacks: when multiple unrelated objects need the same cross-cutting behavior (logging, security checks), the behavior must be duplicated in each object, creating maintenance problems. Aspect-oriented programming (AOP) supplements OOP by addressing concerns horizontally rather than vertically.
What Is AOP: Three Processes
Find join points : determine where in the program cross-cutting logic applies.
Cross-cutting logic (advice) : the business code to be woven, independent of AOP.
Weaving : insert cross-cutting logic into join points.
Developers focus on writing advice; only minimal code declares join points, avoiding per-join-point boilerplate.
Join Points in Code
Example Java class illustrates possible join points:
public class Test {
public static void main(String[] args) {
//@1
B b = new B();
//@2
b.method();
//@3
B.say();
}
static class B {
// field
//@4
private String name;
// constructor
public B() {
//@1.1
}
// instance method
public void method(){
//@2.2
}
// static method
static void say(){
//@3.3
}
}
}Join points fall into two main categories: fields and methods. Methods further divide into constructors, instance methods, static methods. Observable join points include:
Object creation via constructor
Constructor execution
Instance method call
Instance method execution
Static method call
Static method execution
Reflective field read/write
JoinPoint Interface Design
In Java, everything is an object; a class has fields and methods (constructors, instance methods, static methods). AccessibleObject abstracts common behavior. A join point interface must:
Return the current join point: if the join point targets an object (field, instance method, constructor), return both AccessibleObject and the current object (needed for reflective invocation). If it targets a class (static method), return only AccessibleObject.
Control chained invocation of multiple advice — corresponds to Chain of Responsibility. Tomcat's Filter chain uses a collection; Netty's Handler chain uses a linked list. The AOP Alliance uses collection-style chaining.
The AOP Alliance defines Joinpoint:
public interface Joinpoint {
Object proceed() throws Throwable;
Object getThis();
AccessibleObject getStaticPart();
} proceed(): chain to next advice. getThis(): returns current object; returns null for static join points because no instance exists. Spring AOP does not intercept static methods, so in Spring this returns the target (proxied) object. getStaticPart(): returns static part of join point; for methods returns the Method object.
Extensions include sub-interfaces for executable elements (constructors, methods) and fields (AOP Alliance only defines method-level). The alliance's interface hierarchy includes MethodInvocation returning Method.
Advice (Interceptor) Abstraction
Advice needs join point context. The AOP Alliance uses Advice as a tag interface and names the concrete form Interceptor (more descriptive than MethodAdvice). MethodInterceptor receives a method-level join point:
Object invoke(MethodInvocation invocation) throws Throwable;Weaving
Two weaving approaches:
Static weaving : custom class loader modifies class files at load time per weaving rules, then passes modified classes to JVM.
Dynamic weaving : multiple options — dynamic proxy (JDK Proxy) and dynamic bytecode generation (CGLIB).
Spring uses dynamic weaving: generates a proxy object that holds all interceptors for a join point; when the target method is called, the proxy intercepts and executes AOP logic.
A complete flow diagram illustrates the process (image referenced in source).
Spring AOP implements the AOP Alliance interface standard. The alliance defines few interfaces, summarized in a diagram (image referenced).
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.
Architect's Guide
Dedicated to sharing programmer-architect skills—Java backend, system, microservice, and distributed architectures—to help you become a senior architect.
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.
