JDK Dynamic Proxy in Java

Table of contents

1. What is a dynamic proxy?

2. How many ways to implement dynamic proxy?

3. JDK dynamic proxy

4. CGLB dynamic proxy

5. Efficiency of dynamic proxy

6. Why use dynamic proxy?

7. Detailed introduction to the use of JDK dynamic proxy


1. What is a dynamic proxy?

Dynamic proxy is to add new functions to the program by creating proxy objects without changing the original code, so as to realize the function enhancement of the program.

2. How many ways to implement dynamic proxy?

  • JDK dynamic proxy
  • CGLB dynamic proxy

3. JDK dynamic proxy

The InvocationHandler interface, Method class and Proxy class in JDK are used. JDK's dynamic proxy requirements: the target class must have an interface . (The target class is the class where the method we need to enhance the function is located, and it is also the class where the interface implementation class is located). We usually add non-business functions such as logs and transactions to the methods to be enhanced in the target class.

4. CGLB dynamic proxy

The CGLB dynamic agent is Code Generation Library, which is an open source third-party tool library. Its principle is to inherit and generate subclass objects of the target class, so as to enhance the functions of subclasses. But the requirement: the target class cannot be modified by final, and the methods in the target class cannot be modified by final.

5. Efficiency of dynamic proxy

The efficiency of CGLB dynamic proxy is greater than that of JDK dynamic proxy.

6. Why use dynamic proxy?

The dynamic proxy is designed to meet certain needs, because in our actual development process, we often encounter that we need to add methods to the front or back of a written method, or even to several classes of methods. new features. If we directly write the new added method on the method, it will appear messy and redundant.

Then our preliminary improvement can be solved by writing a tool class, writing the methods we want to enhance in a tool class, but requiring these enhanced methods to be statically modified , so that we can use the class name on the original code .The method name is used to add new functions to the original functions. This solves the degree of code confusion to a certain extent, but it is still not perfect.

Improve again : This time is to use dynamic proxy.

This reflects the advantage of dynamic proxy, that is, without modifying any original code, it can enhance the original function.

7. Detailed introduction to the use of JDK dynamic proxy

Since there are not many places where we directly use dynamic proxies in Java projects, we all use dynamic proxies written in the aspectj framework. The dynamic proxy in the aspectj framework is the encapsulated JDK dynamic proxy.

The steps to use JDK dynamic proxy will be introduced in detail below:

  • It is best to create a tool class first, and write the function to be enhanced first, so that we can mobilize the function for enhancement with a single code.
  • Customize the class to implement the InvocationHandler interface, and override the invoke() method.
  • In this custom class, add the Objeck attribute and write a constructor with parameters.
  • Write the following code in the invoke() method

  • Then go to the main class and use dynamic proxy to achieve functional enhancement.

Notice:

The principle of the above JDK dynamic proxy is the reflection mechanism.

The value we can see using proxy.getClass().getName() is com.sun.proxy.$Proxy

Guess you like

Origin blog.csdn.net/weixin_44362089/article/details/127386386