Understanding Java Dynamic Proxy: How JDK Generates and Executes Proxy Classes

This article explains the differences between static and dynamic proxies in Java, details the role of Proxy, InvocationHandler, and the invoke method, and walks through the JDK dynamic proxy creation process with code examples and step‑by‑step analysis.

JavaEdge
JavaEdge
JavaEdge
Understanding Java Dynamic Proxy: How JDK Generates and Executes Proxy Classes

Static proxy classes become cumbersome as the number of methods grows, prompting the use of dynamic proxies which keep proxy code fixed and enable non‑intrusive code extension and AOP programming.

Key components of Java's dynamic proxy are Proxy and InvocationHandler. The InvocationHandler#invoke method receives three parameters: proxy (the proxy instance), method (the method to invoke), and args (method arguments).

JDK Dynamic Proxy Mechanism

The JDK creates a proxy class that implements the target interfaces and extends Proxy. This class (e.g., $Proxy0) contains method stubs that delegate to the InvocationHandler via invoke.

Import the target interface and related classes.

Instantiate the proxy by calling Proxy.newProxyInstance with a class loader, the target interfaces, and an InvocationHandler implementation.

In the invoke method, combine business logic and decide whether to call the original method.

Example Code

proxyObj.business();

When proxyObj.business() is called, the call is routed to InvocationHandler.invoke, where the method parameter is set.

Why It Is Called JDK Dynamic Proxy

The proxy class is generated at runtime by JDK code, not manually written, which is why it is termed a dynamic proxy.

Understanding Proxy Generation

The Proxy.newProxyInstance method performs the following steps:

Uses getProxyClass to locate or generate a proxy class ( $Proxy0) that implements the specified interfaces and extends Proxy.

Instantiates the proxy class via reflection, passing the InvocationHandler to the constructor.

The generated $Proxy0 source includes static Method fields for each method (e.g., m1 for equals, m3 for business) and overrides methods to delegate to the handler:

public final void business() throws ... {
    try {
        super.h.invoke(this, m3, (Object[])null);
    } ...
}

Finally, the proxy instance is cast to the target interface, and invoking its methods triggers the handler's invoke method, which controls execution of the original method.

Note: Proxy#getProxyClass returns the proxy class itself, not the class of the original target object.

Original Source

Signed-in readers can open the original source through BestHub's protected redirect.

Sign in to view source
Republication Notice

This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactadmin@besthub.devand we will review it promptly.

JavaBackend DevelopmentJDKDynamic ProxyInvocationHandler
JavaEdge
Written by

JavaEdge

First‑line development experience at multiple leading tech firms; now a software architect at a Shanghai state‑owned enterprise and founder of Programming Yanxuan. Nearly 300k followers online; expertise in distributed system design, AIGC application development, and quantitative finance investing.

0 followers
Reader feedback

How this landed with the community

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.