1. What is the proxy mode?
Definition of the proxy pattern: For some reason an object needs to be provided with a proxy to control access to that object. In this case, the access object is not suitable or cannot directly reference the target object, and the proxy object acts as an intermediary between the access object and the target object.
To put it simply, we use proxy objects instead of accessing real objects, so that we can provide additional function operations and extend the function of the target object without modifying the original target object.
The main purpose of the proxy mode is to extend the functionality of the target object. For example, you can add custom operations before and after a method of the target object is executed.
The proxy mode has two implementation modes: static proxy and dynamic proxy.
2. Static proxy
In static proxies, we enhance each method of the target object manually. At the JVM level, static proxies turn interfaces, implementation classes, and proxy classes into actual class files at compile time.
Static proxy implementation steps:
- Define an interface and its implementation class;
- Creating a proxy class also implements this interface
- Inject the target object into the proxy class, and then invoke the corresponding method in the target class from the corresponding method in the proxy class.
Dynamic proxy
Dynamic proxies don’t need to manually create a separate proxy class for each target class, and instead of having to implement interfaces, we can implement proxy classes directly. From the JVM’s perspective, dynamic proxies dynamically generate bytecode classes at run time and load them into the JVM.
For Java, there are many ways to implement dynamic proxy, such as JDK dynamic proxy, CGLIB dynamic proxy and so on.
3.1.JDK dynamic proxy implementation steps
- Define an interface and its implementation class;
- Custom InvocationHandler rewrites the Invoke method, where we invoke native methods (methods of the propeted class) and customize some processing logic;
- Through a Proxy newProxyInstance (this loader, Class
[] interfaces, InvocationHandler h) method to create a proxy object;
3.2.CGLIB dynamic proxy implementation steps
- Define a class;
- Custom MethodInterceptor overwrites the Intercept method, which intercepts methods that enhance propped classes, similar to the INVOKE method in JDK dynamic proxies.
- Create the proxy class through the Enhancer class create();
3.3. Differences between JDK dynamic proxies and CGLIB dynamic proxies
- JDK dynamic proxies can only proxy classes that implement interfaces or directly proxy interfaces, whereas CGLIB can proxy classes that don’t implement any interfaces.
- The CGLIB dynamic proxy intercepts method calls of the proxied class by generating a subclass of the proxied class, so classes and methods declared as final cannot be proxied.
4. The difference between static and dynamic proxies
- Flexibility: Dynamic proxies are more flexible in that they don’t have to implement interfaces, they can implement classes directly, and they don’t need to create a proxy class for each target class.
- JVM level: Static agents convert interfaces, implementation classes, and proxy classes into actual class files at compile time. Dynamic proxies, on the other hand, generate bytecodes dynamically at run time and load them into the JVM.