Dynamic proxy implementation in Java
Dynamic proxy is a proxy method that dynamically generates proxy classes at runtime. There are two common implementation methods of dynamic…
Dynamic proxy implementation in Java

Dynamic proxy is a proxy method that dynamically generates proxy classes at runtime. There are two common implementation methods of dynamic proxy.
1. JDK dynamic proxy
JDK dynamic proxy is a technology that uses the java.lang.reflect.Proxy class in the Java standard library to implement dynamic proxy. In JDK dynamic proxy, the proxied class must implement one or more interfaces and implement the specific logic of the proxy class through the InvocationHandler interface.
Specifically, when using JDK dynamic proxy, you need to define a class that implements the InvocationHandler interface and implement the specific logic of the proxy class in the class. Then, create an instance of the proxy class through the Proxy.newProxyInstance() method. This method accepts three parameters: the class loader, the list of interfaces to be implemented by the proxy class, and the InvocationHandler object, as shown in the following code:
import org.example.demo.service.AliPayService;
import org.example.demo.service.PayService;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
public class PayServiceJDKInvocationHandler implements InvocationHandler {
private Object target;
public PayServiceJDKInvocationHandler( Object target) {
this.target = target;
}
//proxy
@Override
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
System.out.println("LOG 1");
System.out.println("LOG 2");
System.out.println("LOG 3");
Object retVal = method.invoke(target, args);
System.out.println("Done");
return retVal;
}
public static void main(String[] args) {
PayService target= new AliPayService();
InvocationHandler handler =
new PayServiceJDKInvocationHandler(target);
PayService proxy = (PayService) Proxy.newProxyInstance(
target.getClass().getClassLoader(),
new Class[]{PayService.class},
handler
);
proxy.pay();
}
}
The advantage of JDK dynamic proxy is that it is simple to implement and easy to understand and master, but its disadvantage is that it can only proxy classes that implement the interface, and cannot proxy classes that do not implement the interface.
2. CGLIB dynamic proxy
CGLIB dynamic proxy is a technology that uses the CGLIB library to implement dynamic proxy. In CGLIB dynamic proxy, the proxy class does not need to implement the interface, but implements the proxy by inheriting the proxied class. Specifically, when using CGLIB dynamic proxy, you need to define a subclass that inherits the proxied class and implement the specific logic of the proxy class in the subclass. Then, create an instance of the proxy class through the Enhancer.create() method. This method accepts a class as a parameter, which represents the class to be proxied, as shown in the following code:
import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import org.example.demo.service.AliPayService;
import org.example.demo.service.PayService;
import java.lang.reflect.Method;
public class PayServiceCGLIBInterceptor implements MethodInterceptor {
private Object target;
public PayServiceCGLIBInterceptor(Object target){
this.target = target;
}
@Override
public Object intercept(Object o, Method method, Object[] args, MethodProxy methodProxy) throws Throwable {
System.out.println("LOG 1");
System.out.println("LOG 2");
System.out.println("LOG 3");
Object retVal = methodProxy.invoke(target, args);
System.out.println("Done");
return retVal;
}
public static void main(String[] args) {
PayService target= new AliPayService();
PayService proxy= (PayService) Enhancer.create(target.getClass(),new PayServiceCGLIBInterceptor(target));
proxy.pay();
}
}
The advantage of CGLIB dynamic proxy is that it can proxy classes that do not implement interfaces, but its disadvantage is that the implementation is relatively complex and requires understanding of how to use the CGLIB library.
Differencec between JDK dynamic proxy and CGLIB dynamic proxy
JDK dynamic proxy and CGLIB dynamic proxy are both common dynamic proxy implementation technologies, but they have the following differences:
- JDK dynamic proxy is based on interface and requires the target object to implement the interface; || CGLIB dynamic proxy is based on class and can proxy target objects that do not implement the interface.
- JDK dynamic proxy uses java.lang.reflect.Proxy and java.lang.reflect.InvocationHandler to generate proxy objects; || CGLIB dynamic proxy uses the CGLIB library to generate proxy objects.
- The proxy object generated by JDK dynamic proxy is the interface implementation of the target object; || the proxy object generated by CGLIB dynamic proxy is a subclass of the target object.
- JDK dynamic proxy has relatively high performance and generates proxy objects quickly; || CGLIB dynamic proxy has relatively low performance and generates proxy objects slowly.
- CGLIB dynamic proxy cannot proxy final classes and final methods; || JDK dynamic proxy can proxy any class.
Summary
In summary, the implementation methods of dynamic proxy mainly include JDK dynamic proxy and CGLIB dynamic proxy. In JDK dynamic proxy, the proxy class must implement one or more interfaces, while in CGLIB dynamic proxy, the proxy class does not need to implement interfaces, but the proxy class cannot be a final type, because it implements dynamic proxy by defining a subclass of the proxied class. Therefore, developers need to choose the appropriate technology to implement dynamic proxy according to specific needs.
메타데이터
- post_id
- e24ca645e2fc
- slug
- dynamicproxy-implementation-in-java-e24ca645e2fc
- url
- https://medium.com/@umeshcapg/dynamicproxy-implementation-in-java-e24ca645e2fc
- canonical_url
- https://medium.com/@umeshcapg/dynamicproxy-implementation-in-java-e24ca645e2fc
- author_url
- https://medium.com/@umeshcapg
- status
- ok
- fetched_at
- 2026-07-11 12:15:36