In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-15 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
This article mainly introduces "how to realize the Java dynamic proxy mode". In the daily operation, I believe many people have doubts about how to realize the Java dynamic proxy mode. The editor consulted all kinds of materials and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to realize the Java dynamic proxy mode". Next, please follow the editor to study!
1. Overview
Java defines its own proxy under the java.lang.reflect package. Using the classes under this package, we can dynamically create a proxy class at run time to implement one or more interfaces. And forward the call to the method to the class you specify. Because the actual agent is created at run time, it is called a dynamic agent.
Proxy: completely generated by java and implements the complete subject interface.
Any method call on InvocationHandler:Proxy is passed into this class, and InvocationHandler controls access to RealSubject.
Because Java has helped us create the Proxy class, we need a way to tell the Proxy class what you are going to do, and we can't write the code into the Proxy class as before, because the Proxy class is not implemented by us. So where should we put it? Put in the InvocationHandler class, the InvocationHandler class is any call that responds to the agent. We can think of an InvocationHandler as an object that the agent requests to do the actual work after receiving the method call.
2. Java.lang.reflect.InvocationHandler
An interface implemented by the proxy instance with only one invoke () method inside. The signature is as follows
Java code
Public Object invoke (Object proxy, Method method, Object [] args)
When the agent's method is called, the agent forwards the call to InvocationHandler, which in turn calls its invoke () method.
3. Java.lang.reflect.Proxy
Provides static methods for creating dynamic proxy classes and instances, which are also superclasses of all dynamic proxy classes created by these methods. The static approach we often use is:
Java code
NewProxyInstance (ClassLoader loader, Class [] interfaces, InvocationHandler h)
4. Example:
Situation: you can view and change the name and gender, but not the rate. Others can check the name, gender and change the rate, but not the name and gender.
4.1 define an interface:
Java code
Public interface Person {String getName (); String getGender (); void setName (String name); void setGender (String gender); void setRate (int rate); int getRate ();}
4.2Definitions implementing Person interface classes
Java code
Public class PersonImpl implements Person {String name; String gender; String interests; int rate; public String getName () {return name;} public void setName (String name) {this.name = name;} public String getGender () {return gender;} public void setGender (String gender) {this.gender = gender } public String getInterests () {return interests;} public void setInterests (String interests) {this.interests = interests;} public int getRate () {return rate;} public void setRate (int rate) {this.rate = rate;}
4. 3 define the OwnerInvocationHandler class, which means that if it is me, you can modify it to see the name and gender.
Java code
Public class OwnerInvocationHandler implements InvocationHandler {private Person personBean; public OwnerInvocationHandler (Person personBean) {this.personBean = personBean } @ Override public Object invoke (Object proxy, Method method, Object [] args) throws IllegalAccessException {try {if (method.getName (). StartsWith ("get")) {/ / if the method name is get, call the corresponding get method return method.invoke (personBean, args) in the person class } else if (method.getName (). Equals ("setRate")) {/ / if the method is setRate, throw an exception throw new IllegalAccessException ("access deny");} else if (method.getName (). StartsWith ("set")) {/ / if it is set, call the corresponding set method return method.invoke (personBean, args) in the person class } else {System.out.println ("non method invoke");}} catch (InvocationTargetException e) {e.printStackTrace ();} return null;}}
Define the NonInvocationHandler class, which means that if not for me, you can check the name, gender and modify the rate.
Java code
Public class NonInvocationHandler implements InvocationHandler {/ / private Person person; public NonInvocationHandler (Person person) {this.person = person } @ Override public Object invoke (Object proxy, Method method, Object [] args) throws Throwable {if (method.getName (). StartsWith ("setRate")) {return method.invoke (person, args);} else if (method.getName (). StartsWith ("get")) {return method.invoke (person, args) } else {System.out.println ("non method invoke"); return null;}
4.5 Test class MyDynamicProxy
Java code
Public class MyDynamicProxy {public Person getOwnerPersonBeanProxy (Person person) {return (Person) Proxy.newProxyInstance (person.getClass (). GetClassLoader (), person.getClass (). GetInterfaces (), new OwnerInvocationHandler (person));} public Person getNonPersonBeanProxy (Person person) {return (Person) Proxy.newProxyInstance (person.getClass (). GetClassLoader (), person.getClass (). GetInterfaces (), new NonInvocationHandler (person)) } public static void main (String [] args) {MyDynamicProxy mdp = new MyDynamicProxy (); mdp.test ();} public void test () {/ / Person person = getPersonBeanFromDB1 (); Person personProxy = getOwnerPersonBeanProxy (person); System.out.println (personProxy.getName ()) Try {personProxy.setRate (2);} catch (Exception e) {System.out.println ("can not setRate");} / / Person person1 = getPersonBeanFromDB1 (); Person personProxy2 = getNonPersonBeanProxy (person1); System.out.println (personProxy2.getName ()); personProxy2.setRate (2) System.out.println (personProxy2.getRate ());} private Person getPersonBeanFromDB1 () {Person pb = new PersonImpl (); pb.setName ("remy"); pb.setGender ("girl"); pb.setRate (1); return pb;}
Output result:
Java code
Remy can not setRate remy 2 at this point, the study on "how to implement the Java dynamic proxy mode" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!
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.