Please go to DobbyKim’s Question of the Day for more questions

A:

Proxy mode refers to “providing a proxy for an object to control access to that object.”

To put it simply, A did something by itself before, but after using the agent, A did not do it directly, but B, the agent of A, did it.

Let’s look at the class diagram for the proxy pattern:

Roles in proxy mode:

  • Subject Indicates the common interface of the target object and the proxy object
  • RealSubject A RealSubject is a role to be proxied and defines the target object represented by the proxy object
  • Proxy Proxy is a Proxy class. The Proxy object has a reference to the target object and can operate the target object at any time

When do we need the proxy model?

  • When we don’t want to directly access an object
  • When you need to make method enhancements to a class

Static proxy versus dynamic proxy:

The proxy mode can be divided into static proxy and dynamic proxy

Static proxies mean that proxy classes generate corresponding class files at compile time, while dynamic proxies are created dynamically at run time through Java’s reflection mechanism.

Examples of static versus dynamic proxy code can be found in my repository: github.com/jinrunheng/…

Static proxy: github.com/jinrunheng/…

JDK dynamic proxy: github.com/jinrunheng/…

CGLIB dynamic Proxy: github.com/jinrunheng/…

What are the differences between CGLIB dynamic proxies and JDK dynamic proxies?

  1. JDK dynamic proxies can only generate proxies for classes that implement interfaces, not for target classes that do not have interfaces. Why are JDK dynamic proxies implemented based on interfaces? The reason is: Java is single inheritance, we generate dynamic Proxy class has inherited the Proxy class, can not inherit other classes, so we can only rely on the implementation of the Proxy class interface to implement this form of Proxy.
  2. CGLIB is a proxy for class implementation. It basically generates a subclass of the target class and overrides its methods to implement bytecode enhancement, but because of inheritance, the class or the method intended to implement enhancement will not succeed if the final keyword is declared.
  3. CGLIB uses the ASM bytecode generation framework and uses bytecode to generate proxy classes. Before JDK6, the efficiency of JDK proxy was higher than that of JDK dynamic proxy. However, with the continuous upgrade and improvement of JDK version, the efficiency of JDK proxy has been greatly improved. Its efficiency is slightly higher than that of CGLIB
  4. Spring AOP provides support for JDK dynamic proxies and CGLIB dynamic proxies. Spring uses JDK dynamic proxies when beans implement interfaces, and CGLIB implementations when beans do not implement interfaces

Application of proxy mode:

The most classic use of the proxy pattern is Spring AOP.

Some common activities in object orientation, such as logging and permission validation, can be duplicated in each business method, resulting in code redundancy. AOP, on the other hand, refers to section-oriented programming, defining an aspect and using the aspect to cut into the corresponding method to weave the relevant logic. Section-oriented programming (AOP) uses the proxy pattern (JDK dynamic proxy, CGLIB dynamic proxy).