Understanding Java Proxy Mechanisms and Spring AOP: Static, Dynamic, and AspectJ
This tutorial explains Java's proxy design pattern, bytecode manipulation, static proxies using AspectJ and JDK, dynamic proxies with JDK and CGLIB, and demonstrates how Spring AOP selects between these mechanisms, providing complete code examples for each approach.
This article introduces Java's proxy mechanism and its application in Spring AOP, covering the proxy design pattern, bytecode loading, static proxies (AspectJ and JDK), dynamic proxies (JDK and CGLIB), and how Spring chooses between them.
1. Proxy Pattern
Explains the classic proxy pattern using a celebrity‑agent analogy, highlighting its advantages such as hiding the delegate implementation and decoupling client from the real subject.
2. Bytecode and Proxy
Describes how Java compiles .java files to .class bytecode and how bytecode can be generated at runtime to create classes dynamically, illustrated with a Javassist example.
public class JavassistDemo {
public static void main(String[] args) {
makeNewClass();
}
// ... (rest of the Javassist code)
}3. Static Proxy
3.1 AspectJ Static Proxy
Shows how to add AspectJ dependencies, define a pointcut and advice, and compile with the AspectJ compiler to produce a statically woven proxy.
<dependency>
org.aspectj
aspectjrt
1.8.9
</dependency>3.2 JDK Static Proxy
Implements a proxy class that implements the same interface as the real subject and adds pre‑ and post‑processing logic.
public class Agent implements ShowService {
private Star star;
private void getMoney(){ System.out.println("get money"); }
private void writeReceipt(){ System.out.println("write receipt"); }
@Override
public void sing(String songName){
getMoney();
star.sing(songName);
writeReceipt();
}
// ...
}4. Dynamic Proxy
4.1 JDK Dynamic Proxy
Uses java.lang.reflect.Proxy and InvocationHandler to create a proxy at runtime.
ShowService proxy = (ShowService) Proxy.newProxyInstance(
star.getClass().getClassLoader(),
star.getClass().getInterfaces(),
new InvocationHandlerImpl(star));
proxy.sing("Mockingbird");4.2 CGLIB Dynamic Proxy
Creates a subclass of the target class using Enhancer and MethodInterceptor, allowing proxying of classes without interfaces.
Enhancer enhancer = new Enhancer();
enhancer.setSuperclass(star.getClass());
enhancer.setCallback(new MethodInterceptorImpl());
ShowService proxy = (ShowService) enhancer.create();
proxy.sing("Mockingbird");5. Spring AOP
Explains that Spring AOP is built on dynamic proxies (JDK or CGLIB) and shows both XML‑based and annotation‑based configurations, including the use of @AspectJ annotations without requiring the AspectJ compiler.
<aop:aspectj-autoproxy proxy-target-class="true"/>6. Summary
The article provides a practical, code‑driven overview of Java proxy techniques and their integration into Spring AOP, helping readers understand when to use static versus dynamic proxies and how Spring decides between JDK and CGLIB implementations.
Selected Java Interview Questions
A professional Java tech channel sharing common knowledge to help developers fill gaps. Follow us!
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.