Understanding Java:Proxy
What is Proxy
The Proxy is a design pattern in software design. It provides a surrogate or placeholder for another object to control access to it. It lets you call a method of a proxy object the same as calling a method of a POJO. But the proxy’s methods can do additional things than POJO’s method.
Why Do We Need Proxy
- It lets you control to access an object. For example, to delay an object’s construction and initialization.
- It lets you track the object access. For example, tracking methods access that can analyze user behavior or analyze system performance.
How to Use Proxy in Java
There are commonly three ways to implement the proxy pattern in Java: static proxy, JDK dynamic proxy, and CGLib dynamic proxy.
Static Proxy
The static proxy implemented by reference the common class in proxy class. The following example is a basic static proxy implementation:
public interface Subject{ |
JDK Dynamic Proxy
The JDK dynamic proxy implemented in package java.lang.reflect
of Java API. Its underlying implementation is the Java reflection.
The manly class for implemented dynamic proxy are: the interface java.lang.reflect.InvocationHandler
, and the class java.lang.reflect.Proxy
.
You can call the Proxy
‘s a static method Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)
to get a proxy object.
- The
ClassLoader
instance commonly useClassLoader.getSystemClassLoader()
. - The array of Class
Class<?>[]
is the interfaces you want to implement. - The
InvocationHandler
instance is an invocation handler that handling proxy method calling with the methodinvoke(Object proxy, Method method, Object[] args)
. We need to create an invocation handler by implementing the interfaceInvocationHandler
.
The following code example is a basic JDK Dynamic proxy implementation:
public interface Subject{ |
Properties of Proxy Classes
- All proxy classes extend the class
java.lang.reflect.Proxy
. - A proxy class has only one instance field–the invocation handler, which is defined in the
Proxy
superclass. - Any additional data required to carry out the proxy objects’ tasks must be stored in the invocation handler. The invocation handler wrapped the actual objects.
- The name of proxy classes are not defined. The
Proxy
class in Java virtual machine generates class names that begin with string $Proxy. - There is only one proxy class for a particular class loader and ordered set of interfaces. If you call the
newProxyInstance
method twice with the same class loader and interface array, you get two objects of the same class. You can get class byProxy.getProxyClass()
. You can test whether a particularClass
object represents a proxy class by calling theisProxyClass
method of theProxy
class.
CGLib Dynamic Proxy
CGLib (Code Generation Library) is an open source library that capable creating and loading class files in memory during Java runtime. To do that it uses Java bytecode generation library ‘asm’, which is a very low-level bytecode creation tool. CGLib can proxy to objects without Interface.
<dependency> |
How to Use CGLib
To create a proxy object using CGLib is almost as simple as using the JDK reflection proxy API. The following code example is a basic CGLib Dynamic proxy implementation:
public interface Subject{ |
The difference between JDK dynamic proxy and CGLib is that name of the classes are a bit different and CGLib do not have an interface.
It is also important that the proxy class extends the original class and thus when the proxy object is created it invokes the constructor of the original class.
Conclusion
Differences between JDK proxy and CGLib:
- JDK Dynamic proxy can only proxy by interface (so your target class needs to implement an interface, which is then also implemented by the proxy class). CGLib (and javassist) can create a proxy by subclassing. In this scenario the proxy becomes a subclass of the target class. No need for interfaces.
- JDK Dynamic proxy generates dynamically at runtime using JDK Reflection API. CGLib is built on top of ASM, this is mainly used the generate proxy extending bean and adds bean behavior in the proxy methods.
References
[1] Core Java Volume I Fundamentals by S, Horstmann
[2] Design Patterns: Elements of Reusable Object-Oriented Software by Erich Gamma, Richard Helm, Ralph Johnson, John Vlissides
[4] cglib - GitHub
[6] Creating a Proxy Object Using cglib
[7] What is the difference between JDK dynamic proxy and CGLib? - Stack Overflow