Backend Development 22 min read

Understanding Java Proxy Pattern: Static vs Dynamic Proxies, JDK Proxy and CGLIB

This article explains the Java proxy pattern using a real‑world analogy, compares static and dynamic proxies, details implementations with JDK Proxy and CGLIB (including code samples), and discusses how dynamic proxies support the Open‑Closed principle and are applied in Spring AOP for transaction management.

Full-Stack Internet Architecture
Full-Stack Internet Architecture
Full-Stack Internet Architecture
Understanding Java Proxy Pattern: Static vs Dynamic Proxies, JDK Proxy and CGLIB

The article begins with a story about a customer (Xiao Ming) and an agent (Xiao Hong) buying perfume, using it to illustrate the classic proxy pattern where the agent acts as a proxy for the supplier.

It then defines the two main types of proxies in Java: static proxy and dynamic proxy , listing their advantages and disadvantages. Static proxies are simple but violate the Open‑Closed principle when new requirements appear; dynamic proxies provide better extensibility by generating proxy classes at runtime.

Implementation steps for a static proxy are shown with Java interfaces and classes such as SellPerfume , ChanelFactory , and XiaoHongSellProxy . Sample code demonstrates how the client invokes the proxy to sell perfume.

The article then introduces dynamic proxies, explaining the JDK’s Proxy and InvocationHandler mechanism. It provides a SellProxyFactory class that implements InvocationHandler and overrides invoke() , followed by client code that creates a proxy instance via Proxy.newProxyInstance .

Next, the CGLIB approach is described. After adding the cglib-nodep dependency, a proxy factory implementing MethodInterceptor is created. The factory uses Enhancer to generate a subclass proxy, with an intercept() method that adds pre‑ and post‑processing logic.

A comparison table highlights the similarities and differences between JDK Proxy and CGLIB, noting that CGLIB can proxy concrete classes (except final ones) while JDK Proxy requires interfaces.

Finally, the article connects dynamic proxies to real‑world usage in Spring AOP, showing how annotations like @Transactional rely on proxy mechanisms to wrap method execution with transaction management, logging, and other cross‑cutting concerns.

Overall, the piece demonstrates how static and dynamic proxies work in Java, why dynamic proxies better satisfy the Open‑Closed principle, and how they are employed in frameworks such as Spring for AOP features.

JavaSpring AOPDynamic ProxyProxy PatternCglibJDK Proxy
Full-Stack Internet Architecture
Written by

Full-Stack Internet Architecture

Introducing full-stack Internet architecture technologies centered on Java

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.