[introduction to Java] Day11 Java agent – JDK dynamic agent

Today, let's take a look at another proxy method of Java - JDK dynamic proxy

The proxy method we introduced earlier is called static proxy, that is, generating proxy objects statically, while dynamic proxy creates proxy objects at runtime. Dynamic agent has a more powerful function of intercepting requests, because it can obtain the runtime information of the class and obtain more powerful execution (operation) according to the runtime information.

Let's take the above example as an example. The istars interface and stars class here do not need to be modified, only the proxy class needs to be modified.

To create a JDK dynamic proxy, you need to implement the invocationhandler interface and rewrite the invoke method. The specific steps are as follows:

  1. Create a class that implements the invocationhandler interface.

  2. Provide proxy class with classloader and interfaces of delegate class to create dynamic proxy class.

  3. The constructor of dynamic proxy class is obtained by reflection mechanism.

  4. Create a dynamic proxy class object using the constructor of the dynamic proxy class.

Let's use dynamic proxy to transform the previous class:

The interface and delegate classes do not need to be modified:

This is the proxy class after using dynamic proxy:

Create a new factory class to return the proxy instance:

Rewrite the test class:

The output is as follows:

When using dynamic proxy, the invocationhandler interface is implemented and the invoke method is rewritten. The three parameters of the invoke method are:

The details of proxy's newproxyinstance method are as follows:

It can be seen that the dynamic proxy here, like the static proxy, saves an instance of the delegate class in the proxy class. In fact, it calls the original delegate instance to perform the required operations. The proxy class is equivalent to adding a shell to the delegate class and placing the delegate class inside the proxy class, so as to control the client class's access to the delegate class, just as in the above example, The proxy class intercepts the client class's access to the dance method of stars class and outputs supplementary information.

The biggest difference between dynamic agent and static agent is that the generation period of agent class is different. Static agent is in compilation period, while dynamic agent is dynamically generated according to delegate class information at runtime.

Secondly, the dynamic agent implements the invocationhandler interface, while the static agent directly implements the public interface. Of course, the dynamic proxy also needs to implement the same interface, but the interface information is placed inside getInstance, which is equivalent to the agreement between the proxy class and the delegate class. "These methods can help me proxy.".

Finally, dynamic agents can get more runtime information and be more flexible to use.

So far, the explanation of JDK dynamic agent is completed. Welcome to continue your attention!

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>