Designing AOP Interfaces: From Joinpoint Basics to Spring Implementation
This article explores how to design module interfaces using Aspect‑Oriented Programming in Java, covering AOP background, definition, pointcut identification, joinpoint interface design, advice abstraction, and both static and dynamic weaving techniques, with practical code examples and diagrams.
AOP Background
Object‑oriented programming (OOP) can lead to duplicated code when multiple unrelated classes need the same cross‑cutting behavior such as logging or security checks. Aspect‑Oriented Programming (AOP) addresses this by providing a horizontal programming model, complementing the vertical nature of OOP.
What Is AOP
AOP focuses on three processes: identifying pointcuts (where to apply cross‑cutting logic), defining the cross‑cutting logic (advice), and weaving that logic into the target code.
Identify pointcut: determine the program locations for cross‑cutting logic.
Cross‑cutting logic (business code): the actual code to be applied.
Weaving: integrate the cross‑cutting logic into the identified points.
Developers mainly need to write the cross‑cutting logic and specify pointcuts, without manually inserting code at each location.
Pointcut Identification
In Java, everything is an object. Classes contain fields and methods (constructors, instance methods, static methods). The AccessibleObject abstraction represents these members. Pointcut interfaces must return the current pointcut, which can be either an AccessibleObject alone (for static contexts) or both the object instance and the AccessibleObject (for instance contexts).
Pointcuts can also control the chaining of multiple advices, similar to the Chain‑of‑Responsibility pattern used in servlet filters or Netty handlers.
public interface Joinpoint {
Object proceed() throws Throwable;
Object getThis();
AccessibleObject getStaticPart();
} Object proceed(): invoke the next advice in the chain. Object getThis(): return the target object for instance pointcuts; returns null for static pointcuts. AccessibleObject getStaticPart(): return the reflective object (e.g., Method) representing the static part of the joinpoint.
Advice Abstraction
Advice needs joinpoint information to know where to apply. In the AOP Alliance, advice is represented by interceptors; a MethodInterceptor receives a MethodInvocation containing the joinpoint.
Object invoke(MethodInvocation invocation) throws Throwable;Weaving
Two weaving approaches exist:
Static weaving: a custom class loader modifies bytecode during class loading according to weaving rules.
Dynamic weaving: uses runtime proxies (JDK Proxy) or bytecode generation libraries (cglib) to create proxy objects that apply advices.
Spring AOP employs dynamic weaving by generating proxy objects that hold a list of interceptors; method calls on the proxy trigger the advice chain.
Illustrative Diagrams
The AOP Alliance defines a small set of interfaces, as shown below:
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.
Java High-Performance Architecture
Sharing Java development articles and resources, including SSM architecture and the Spring ecosystem (Spring Boot, Spring Cloud, MyBatis, Dubbo, Docker), Zookeeper, Redis, architecture design, microservices, message queues, Git, etc.
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.
