This article mainly introduces the relevant information about the example explanation of the dynamic proxy mechanism in java. I hope that through this article everyone can understand and master the dynamic proxy mechanism. Friends in need can refer to the following
Dynamics in java Example explanation of proxy mechanism
When learning Spring, we know that Spring mainly has two major ideas, one is IoC and the other is AOP. For IoC, it goes without saying much about dependency injection, and For Spring's core AOP, we not only need to know how to satisfy our functions through AOP, but we also need to learn what its underlying principles are, and the principle of AOP is Java's dynamic proxy mechanism, so this article 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 are used to implement our dynamic proxy. Must be used. First, let’s take a look at how the Java API help document describes these two classes:
InvocationHandler:
1 2 3 |
|
Each dynamic The proxy class must implement the InvocationHandler interface, and each proxy class instance 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. to make the call. Let's take a look at the only method of the InvocationHandler interface, the invoke method:
1 |
|
We see that this method accepts a total of three parameters, so what do these three parameters represent? ?
proxy: refers to the real object we are proxying
method: refers to a method of the real object we want to call Method object
args: refers to the parameters accepted when calling a method of the real object
If you don’t understand it clearly , these parameters will be explained in depth through an example later.
Next let’s take a look at the Proxy class:
1 2 |
|
The function of the Proxy class is to dynamically create a proxy object. It provides There are many methods, but the one we use most is the newProxyInstance method:
1 2 3 4 |
|
The function of this method is to get a dynamic proxy object, which receives three parameters, Let’s take a look at what these three parameters represent:
loader: A ClassLoader object that defines which ClassLoader object will load the generated proxy object
interfaces: An array of Interface objects, which represents a set of interfaces that I will provide to the object I need to proxy. If I provide a set of interfaces to it, then the proxy object will claim to implement it. This interface (polymorphic), so that I can call the methods in this set of interfaces
h: An InvocationHandler object, which represents when my dynamic proxy object is calling the method time, which InvocationHandler object will be associated
Okay, after introducing these two interfaces (classes), let’s take a look at our dynamic proxy mode through an example. What does it look like:
First we define an interface of Subject type and declare two methods for it:
1 2 3 4 5 6 |
|
Then, A class is defined to implement this interface. This class is our real object, RealSubject class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
Next, we will define a dynamic proxy class, as shown above To put it bluntly, every dynamic proxy class must implement the InvocationHandler interface, so our dynamic proxy class is no exception:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
|
Finally, let’s take a look at our Client Class:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 |
|
Let’s first take a look at the console output:
1 2 3 4 5 6 7 8 9 10 11 |
|
Let’s first take a look at $ Proxy0, we see that this thing is printed by the statement System.out.println(subject.getClass().getName());, so why is the class name of the proxy object we return like this? Woolen cloth?
1 2 |
|
Maybe I thought that the returned proxy object would be a Subject type object, or an InvocationHandler object, but it turned out not to be the case. First, let’s explain why we can use Convert it to an object of type Subject? The reason is that in the second parameter of the newProxyInstance method, we provide a set of interfaces for this proxy object, then my proxy object will implement this set of interfaces. At this time, of course we can force the type conversion of this proxy object. It is any one of this set of interfaces. Because the interface here is of Subject type, it can be converted into Subject type.
同时我们一定要记住,通过 Proxy.newProxyInstance 创建的代理对象是在jvm运行时动态生成的一个对象,它并不是我们的InvocationHandler类型,也不是我们定义的那组接口的类型,而是在运行是动态生成的一个对象,并且命名方式都是这样的形式,以$开头,proxy为中,最后一个数字表示对象的标号。
接着我们来看看这两句
1 2 |
|
这里是通过代理对象来调用实现的那种接口中的方法,这个时候程序就会跳转到由这个代理对象关联到的 handler 中的invoke方法去执行,而我们的这个 handler 对象又接受了一个 RealSubject类型的参数,表示我要代理的就是这个真实对象,所以此时就会调用 handler 中的invoke方法去执行:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
|
我们看到,在真正通过代理对象来调用真实对象的方法的时候,我们可以在该方法前后添加自己的一些操作,同时我们看到我们的这个 method 对象是这样的:
1 2 3 |
|
正好就是我们的Subject接口中的两个方法,这也就证明了当我通过代理对象来调用方法的时候,起实际就是委托由其关联到的 handler 对象的invoke方法中来调用,并不是自己来真实调用,而是通过代理的方式来调用的。
这就是我们的java动态代理机制。
The above is the detailed content of Detailed introduction to dynamic proxy mechanism in Java. For more information, please follow other related articles on the PHP Chinese website!