Detailed examples of Java Dynamic Proxy Mechanism

Java Dynamic Proxy Mechanism

When learning spring, we know that spring has two main ideas, one is IOC and the other is AOP. For IOC, we don't need to say much about dependency injection. For spring's core AOP, we not only need to know how to meet our functions through AOP, but also need to learn the underlying principle, The principle of AOP is Java's dynamic proxy mechanism, so this essay is a review of Java's dynamic mechanism.

In the dynamic proxy mechanism of Java, there are two important classes or interfaces, one is invocationhandler (Interface) and the other is proxy (class). This class and interface must be used to implement our dynamic proxy. First, let's take a look at how the API help document of Java describes these two classes:

InvocationHandler:

Each dynamic proxy class must implement the invocationhandler interface, and the instance of each proxy class is associated with a handler. When we call a method through the proxy object, the call of this method will be forwarded to the invoke method of the invocationhandler interface. Let's take a look at the invoke method, the only method of the invocationhandler interface:

We see that this method accepts three parameters. What do these three parameters represent?

Proxy: refers to the real object we proxy. Method: refers to the method object of a method of the real object we want to call. Args: refers to the parameters accepted when calling a method of the real object

If you don't understand it very well, these parameters will be explained further through an example later.

Next, let's take a look at the proxy class:

The proxy class is used to dynamically create a proxy object class. It provides many methods, but the most commonly used method is newproxyinstance:

The function of this method is to get a dynamic proxy object, which receives three parameters. Let's see the meaning of these three parameters:

Well, after introducing these two interfaces (classes), let's take a look at our dynamic proxy mode through an example:

First, we define an interface of subject type and declare two methods for it:

Next, a class is defined to implement this interface. This class is our real object, realsubject class:

Next, we will define a dynamic proxy class. As mentioned earlier, each dynamic proxy class must implement the invocationhandler interface, so our dynamic proxy class is no exception:

Finally, let's take a look at our client class:

Let's first look at the console output:

Let's first look at $proxy0. We see that this thing is made by system out. println(subject.getClass(). getName()); This statement is printed out, so why is the class name of the proxy object we return like this?

Maybe I thought the returned proxy object would be an object of subject type or an object of invocationhandler, but the result is not. First, let's explain why we can convert it into an object of subject type here? The reason is that on the second parameter of the newproxyinstance method, we provide a set of interfaces for the proxy object, so my proxy object will implement this set of interfaces. At this time, of course, we can convert the mandatory type of the proxy object to any one of these interfaces, because the interface here is of subject type, So you can convert it to the subject type.

At the same time, we must remember that through proxy The proxy object created by newproxyinstance is an object dynamically generated when the JVM runs. It is not our invocationhandler type or the type of the group of interfaces we define, but an object dynamically generated when running, and the naming method is in this form. It starts with $, proxy is the middle, and the last number represents the label of the object.

Then let's look at these two sentences

Here is the method in the interface implemented through the proxy object. At this time, the program will jump to the invoke method in the handler associated with the proxy object for execution, and our handler object accepts a parameter of realsubject type, indicating that the real object I want to proxy, Therefore, the invoke method in the handler will be called to execute:

We can see that when we call the method of the real object through the proxy object, we can add some operations before and after the method. At the same time, we can see that our method object is as follows:

It happens to be the two methods in our subject interface, which proves that when I call a method through a proxy object, I actually delegate it to the invoke method of the handler object associated with it. It is not really called by myself, but by proxy.

This is our Java dynamic proxy mechanism.

This essay explains the dynamic proxy mechanism in Java in detail. This knowledge point is very important, including our spring AOP, which is realized through the dynamic proxy mechanism, so we must have a good understanding of the dynamic proxy mechanism.

If you have any questions, please leave a message or go to the community of this site for exchange and discussion. Thank you for reading. I hope it can help you. Thank you for your support to this site!

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
分享
二维码
< <上一篇
下一篇>>