Understanding AOP: From Joinpoint Design to Spring’s Dynamic Weaving
This article analyzes AOP’s origins, defines join points and cross‑cutting logic, designs interfaces based on the AOP Alliance specifications, and compares static versus dynamic weaving approaches, illustrating each step with Java code examples and diagrams to show how Spring implements AOP.
Motivation for Aspect‑Oriented Programming
Object‑oriented programming (OOP) repeats code when many unrelated classes need the same behavior such as logging or security checks. Aspect‑Oriented Programming (AOP) introduces a horizontal dimension that isolates these cross‑cutting concerns from the vertical class hierarchy, reducing duplication and improving maintainability.
AOP Process Overview
Identify join points – the exact locations in the program where cross‑cutting logic should be applied.
Define the cross‑cutting logic (advice) – the code that will run at those points.
Weave the advice into the join points – either at compile/load time (static weaving) or at runtime (dynamic weaving).
Join‑point Classification Example
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
}
}
}From this snippet two major join‑point categories emerge:
Fields
Methods – further divided into constructors, instance methods, and static methods.
Goal 1 – Enumerating Java Join Points
Typical join points in Java code include:
Object creation via constructors
Constructor execution
Instance‑method invocation
Instance‑method execution
Static‑method invocation
Static‑method execution
Reflection‑based field read/write
Goal 2 – Designing a Join‑point Interface
To describe a join point an interface must be able to return:
If the join point applies to an object (field, instance method, constructor), both the AccessibleObject and the target instance are required because reflective invocation needs the instance.
If the join point applies to a class (static method or class‑level field), only the AccessibleObject is needed.
When multiple advices may run at the same join point, the interface should support a chain‑of‑responsibility pattern (e.g., Tomcat filters, Netty handlers). The AOP Alliance defines the Joinpoint interface to meet these requirements:
public interface Joinpoint {
Object proceed() throws Throwable;
Object getThis();
AccessibleObject getStaticPart();
} proceed()triggers the next advice in the chain. getThis() returns the current target object (null for static join points). getStaticPart() returns the underlying reflective object such as a Method instance.
The AOP Alliance also defines sub‑interfaces for constructors and methods that extend Joinpoint. Fields are not defined in the original spec but can be added analogously.
Goal 3 – Abstracting Advice (Enhancement)
Advice is the cross‑cutting code executed at a join point. The AOP Alliance uses the term “interceptor” for method‑level advice, but the concept can be expressed as MethodAdvice. The MethodInterceptor interface receives a MethodInvocation (a Joinpoint sub‑interface) and returns the result:
Object invoke(MethodInvocation invocation) throws Throwable;This design follows the chain‑of‑responsibility pattern: each interceptor may call invocation.proceed() to continue the chain.
Goal 4 – Weaving the Advice
Weaving can be performed in two ways:
Static weaving : a custom class loader modifies the bytecode during class loading, inserting the advice directly into the class file.
Dynamic weaving : at runtime, either JDK dynamic proxies or bytecode‑generation libraries such as CGLIB create proxy objects that hold a list of interceptors. Spring AOP adopts the dynamic approach.
The dynamic weaving process used by Spring creates a proxy object, stores the applicable interceptors, and delegates method calls through the interceptor chain before invoking the target method.
Summary of AOP Alliance Interfaces
Spring’s AOP implementation relies on a small set of interfaces defined by the AOP Alliance. Understanding these interfaces clarifies how join points, advices, and weaving interact.
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
Professional architect sharing high‑quality architecture insights. Topics include high‑availability, high‑performance, high‑stability architectures, big data, machine learning, Java, system and distributed architecture, AI, and practical large‑scale architecture case studies. Open to ideas‑driven architects who enjoy sharing and learning.
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.
