In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-03-04 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article introduces the relevant knowledge of "how to understand the dynamic agent mechanism of Java". In the operation of actual cases, many people will encounter such a dilemma, so let the editor lead you to learn how to deal with these situations. I hope you can read it carefully and be able to achieve something!
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 necessary to implement our dynamic proxy. First, let's take a look at how java's API help documentation describes these two classes:
InvocationHandler:
1InvocationHandler is the interface implemented by the invocation handler of a proxy instance.2 3Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance, the method invocation is encoded and dispatched to the invoke method of its invocation handler.
Every dynamic proxy class must implement the InvocationHandler interface, and each instance of the proxy class is associated with a handler. When we call a method through the proxy object, the call to this method will be forwarded to the invoke method of the InvocationHandler interface. Let's take a look at the only method of the InvocationHandler interface, the invoke method:
1Object invoke (Object proxy, Method method, Object [] args) throws Throwable
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 represent.
Method: refers to the Method object that we want to call a method of the real object
Args: refers to the parameters accepted when a method of a real object is called
If you don't quite understand, you will explain these parameters more deeply later through an example.
Let's take a look at the class Proxy:
1Proxy provides static methods for creating dynamic proxy classes and instances, and it is also the superclass of all dynamic proxy classes created by those methods.
The function of the class Proxy is to dynamically create a class of proxy objects. It provides a lot of methods, but the method we use most is newProxyInstance:
1public static Object newProxyInstance (ClassLoader loader, Class [] interfaces, InvocationHandler h) throws IllegalArgumentException
1Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.
The purpose of this method is to get a dynamic proxy object that receives three parameters. Let's take a look at what these three parameters represent:
Loader: a ClassLoader object that defines which ClassLoader object loads the generated proxy object
Interfaces: an array of Interface objects that represents what set of interfaces I'm going to provide to the objects I need to proxy. If I provide a set of interfaces to it, then the proxy object claims to implement that interface (polymorphism) so that I can call the methods in this set of interfaces
H: an InvocationHandler object that represents which InvocationHandler object my dynamic proxy object will be associated with when calling a method
Well, after introducing these two interfaces (classes), let's take a look at what our dynamic proxy pattern looks like through an example:
First, we define an interface of type Subject and declare two methods for it:
1public interface Subject2 {3 public void rent (); 4 5 public void hello (String str); 6}
Next, we define a class to implement this interface, which is our real object, the RealSubject class:
01public class RealSubject implements Subject02 {03 @ Override04 public void rent () 05 {06 System.out.println ("I want to rent my house"); 07} 08 09 @ Override10 public void hello (String str) 11 {12 System.out.println ("hello:" + str); 13} 14}
Next, we will define a dynamic proxy class. As mentioned earlier, every dynamic proxy class must implement the InvocationHandler interface, so our dynamic proxy class is no exception:
01public class DynamicProxy implements InvocationHandler02 {03 / / this is the real object 04 private Object subject;05 06 / / constructor we want to proxy, which assigns the initial value 07 public DynamicProxy (Object subject) 08 {09 this.subject = subject to the real object we want to proxy. 10} 11 12 @ Override13 public Object invoke (Object object, Method method, Object [] args) 14 throws Throwable15 {16 / / We can add some of our own operations 17 System.out.println ("before rent house") before delegating real objects; 18 19 System.out.println ("Method:" + method) 20 21 / / when the proxy object calls the method of the real object, it automatically jumps to the invoke method of the handler object associated with the proxy object to call 22 method.invoke (subject, args); 23 24 / after proxying the real object, we can also add some of our own operations 25 System.out.println ("after rent house"); 26 27 return null;28} 29 30}
Finally, take a look at our Client class:
01public class Client02 {03 public static void main (String [] args) 04 {05 / / the real object we want to proxy 06 Subject realSubject = new RealSubject (); 07 08 / / We pass in the real object which we want to proxy, and finally 09 InvocationHandler handler = new DynamicProxy (realSubject) that calls its method through the real object. 10 11 / * 12 * create our proxy object through the newProxyInstance method of Proxy. Let's take a look at its three parameters 13 * the first parameter handler.getClass (). GetClassLoader (). Here we use the ClassLoader object of the class handler to load our proxy object 14 * second parameter realSubject.getClass (). GetInterfaces (). The interface we provide for the proxy object is the interface implemented by the real object. Indicates that I am proxying the real object so that I can call the method 15 * third parameter handler in this set of interfaces. Here we associate the proxy object with the above InvocationHandler object 16 * / 17 Subject subject = (Subject) Proxy.newProxyInstance (handler.getClass (). GetClassLoader (), realSubject18 .getClass (). GetInterfaces (), handler) 19 20 System.out.println (subject.getClass (). GetName ()); 21 subject.rent (); 22 subject.hello ("world"); 23} 24}
Let's first look at the output of the console:
01 $Proxy002 03before rent house04Method:public abstractvoid com.xiaoluo.dynamicproxy.Subject.rent () 05i want to rent my house06after rent house07 08before rent house09Method:public abstractvoidcom.xiaoluo.dynamicproxy.Subject.hello (java.lang.String) 10hello: world11after rent house
Let's first take a look at the $Proxy0 thing, which we see is printed by System.out.println (subject.getClass (). GetName ());, so why does the class name of the proxy object we return look like this?
1Subject subject = (Subject) Proxy.newProxyInstance (handler.getClass () .getClassLoader (), realSubject2 .getClass () .getInterfaces (), handler)
Maybe I thought the proxy object returned would be an object of type Subject or an object of type InvocationHandler, but it wasn't. First of all, let's explain why we can convert it to an object of type Subject here. The reason is that on the second parameter of the newProxyInstance method, we provide a set of interfaces to the proxy object, then my proxy object will implement this set of interfaces. At this time, of course, we can convert the proxy object force type to any one of this set of interfaces, because the interface here is Subject type, so we can convert it to Subject type.
At the same time, we must keep in mind that the proxy object created through Proxy.newProxyInstance is an object dynamically generated during the jvm runtime, it is not our InvocationHandler type, nor is it the type of the set of interfaces we define, but is running a dynamically generated object, and the naming is in this form, starting with $, proxy, and the last number represents the label of the object.
Then let's take a look at these two sentences.
Subject.rent (); subject.hello ("world")
Here, the method in the interface implemented by the proxy object is called. At this time, the program jumps to the invoke method in the handler associated with the proxy object, and our handler object accepts a parameter of type RealSubject, indicating that I want to proxy the real object, so the invoke method in handler is called to execute:
01public Object invoke (Object object, Method method, Object [] args) 02 throws Throwable03 {04 / / We can add some of our own operations 05 System.out.println ("before rent house") before proxying real objects; 06 07 System.out.println ("Method:" + method) 08 09 / / when the proxy object calls the method of the real object, it automatically jumps to the invoke method of the handler object associated with the proxy object to call 10 method.invoke (subject, args); 11 12 / after proxying the real object, we can also add some of our own operations 13 System.out.println ("after rent house"); 14 15 return null;16}
We see that when we actually call a method of a real object through a proxy object, we can add some of our own operations before and after the method, and we see that our method object looks like this:
1public abstract void com.xiaoluo.dynamicproxy.Subject.rent () 2 3public abstract void com.xiaoluo.dynamicproxy.Subject.hello (java.lang.String)
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 to which it is associated, not by myself, but by proxy.
This is the end of the content of "how to understand Java dynamic proxy mechanism". Thank you for your reading. If you want to know more about the industry, you can follow the website, the editor will output more high-quality practical articles for you!
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.