Dynamic proxy (1) ---- JDK dynamic proxy

Source: Internet
Author: User

In Java. lang. the reflect package provides a Proxy class and an InvocationHandler interface. By using this class and interface, you can generate JDK dynamic Proxy class or dynamic Proxy object. proxy provides a static method for creating dynamic Proxy classes and Proxy objects. It is also the parent class of all dynamic Proxy classes. if we dynamically generate an implementation class for one or more interfaces in the program, we can use Proxy to create a dynamic Proxy class: if we need to dynamically create an instance for one or more interfaces, you can also use the Proxy class to create a dynamic Proxy instance. proxy provides the following two methods to create a dynamic Proxy class and a dynamic Proxy instance: 1. staticClass <?> GetProxyClass (ClassLoader loader, Class <?>... Interfaces): Creates a Class object corresponding to the dynamic proxy Class, which implements multiple interfaces specified by interfaces. the first ClassLoader specifies the class loader that generates the dynamic proxy class. 2. staticObject newProxyInstance (ClassLoader loader, Class <?> [] Interfaces, InvocationHandler): directly creates a dynamic proxy object. The implementation class of this proxy object implements the series of interfaces specified by interfaces, each method of the proxy object will be replaced by the InvocationHandler object's invoke method. in fact, even if the first method is used to obtain a dynamic proxy class, an InvocationHandler object is required when the program needs to create an object through this proxy class. that is to say, each proxy object generated by the system has an associated InvocationHandler object. the dynamic proxy object generated in the program can be a dynamic proxy class, and then an object is created through the dynamic proxy class to generate a dynamic proxy object. the following code snippet [java] // creates an InvocationHandler object InvocationHandler handler = new MyInvocationHandler (...); // use Pro Xy generates a dynamic Proxy Class proxyClass = Proxy. getProxyClass (Foo. class. getClassLoader (), new Class [] {Foo. class}); // obtain the Constructor with the InvocationHandler parameter in the proxyClass class <T> ctor = proxyClass. getConstructor (new Class [] {InvocationHandler. class}); // call the ctor's newInstance method to create a dynamic instance Foo f = (Foo) ctor. newInstance (new Object [] {handler}); // create an InvocationHandler Object InvocationHandler handler = new MyInvocationHandler (... ); // Use Proxy to generate a dynamic Proxy class proxyClassClass proxyClass = Proxy. getProxyClass (Foo. class. getClassLoader (), new Class [] {Foo. class}); // obtain the Constructor with the InvocationHandler parameter in the proxyClass class <T> ctor = proxyClass. getConstructor (new Class [] {InvocationHandler. class}); // call the ctor's newInstance method to create a dynamic instance Foo f = (Foo) ctor. newInstance (new Object [] {handler}); The above code can be simplified to the following code [java] // create an InvocationHandler Object InvocationHandler hanlder = n Ew MyInvocationHandler (...); // use Proxy to directly generate a dynamic Proxy object Foo f = (Foo) Proxy. newProxyInstance (Foo. class. getClassLoader (), new Class [] {Foo. class}, handler); // create an InvocationHandler object InvocationHandler hanlder = new MyInvocationHandler (...); // use Proxy to directly generate a dynamic Proxy object Foo f = (Foo) Proxy. newProxyInstance (Foo. class. getClassLoader (), new Class [] {Foo. class}, handler); The following program demonstrates Using Proxy and InvocationHandler to generate a dynamic Proxy object [java] public clas S ProxyTest {public static void main (String [] args) throws Exception {// create an InvocationHandler object InvocationHandler handler = new MyInvokationHandler (); // use the specified InvocationHanlder to generate a dynamic Proxy object PersonPro p = (PersonPro) Proxy. newProxyInstance (PersonPro. class. getClassLoader (), new Class [] {PersonPro. class}, handler); // call the walk () method of the dynamic proxy object and the sayHello () method p. walk (); p. sayHello ("luck") ;}} interface PersonPro {void w Alk (); void sayHello (String name);} class MyInvokationHandler implements InvocationHandler {/** replace all methods of the dynamic proxy object with the following invoke Method: * proxy -- represents the dynamic proxy Object * method -- represents the Method being executed * args -- represents the real parameter */public Object invoke (Object proxy, method, object [] args) {System. out. println ("method being executed:" + method); if (args! = Null) {System. out. println ("The following is the real parameter passed in when this method is executed:"); for (Object val: args) {System. out. println (val) ;}} else {System. out. println ("calling this method does not require real parameters");} return null;} public class ProxyTest {public static void main (String [] args) throws Exception {// create an InvocationHandler object InvocationHandler handler = new MyInvokationHandler (); // use the specified InvocationHanlder to generate a dynamic Proxy object PersonPro p = (PersonPro) Proxy. newProxyInstance (PersonPr O. class. getClassLoader (), new Class [] {PersonPro. class}, handler); // call the walk () method of the dynamic proxy object and the sayHello () method p. walk (); p. sayHello ("luck") ;}} interface PersonPro {void walk (); void sayHello (String name );} class MyInvokationHandler implements InvocationHandler {/** when all methods of the dynamic proxy object are executed, they are replaced with the following invoke Method: * proxy -- represents the dynamic proxy Object * method -- represents the Method being executed * args -- represents the real parameter */public Object invoke (Object proxy, method, Object [] Args) {System. out. println ("method being executed:" + method); if (args! = Null) {System. out. println ("The following is the real parameter passed in when this method is executed:"); for (Object val: args) {System. out. println (val) ;}} else {System. out. println ("calling this method does not require real parameters");} return null;} The above program provides a Person interface, which contains two Abstract METHODS: walk and sayHello, next, the program defines a simple InvocationHandler implementation class. When defining this implementation class, the invoke method needs to be rewritten. When executing all the methods of the proxy object, the invoke method will be replaced by the method used to execute it. the running result shows that no matter whether the program executes the walk () method of the proxy object or the sayHello () method of the proxy object, the InvocationHandler object's invoke () method is actually executed.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.