JDK dynamic proxy uses reflection to generate

It provides a Proxy and a class in the Java interfaces InvocationHandler java.lang.reflect package may be generated dynamically or JDK dynamic proxy class through the use of this proxy object classes and interfaces.

Create a dynamic proxy to use Proxy and InvocationHandler

Proxy provides static methods for creating dynamic proxy class and proxy objects, it is also the parent of all the dynamic generation of class. If one or more interface implementation classes dynamically generated program, can be used to create dynamic Proxy proxy class; if desired to create an instance of one or more interfaces dynamically, may be used to create a dynamic Proxy agent instance.

Proxy provides the following two methods to create a dynamic proxy classes and dynamic proxy instance.

  • static Class getProxyClass (ClassLoader loader, Class ... interfaces <?>) <?>: create a dynamic proxy class Class object corresponding to the proxy class interfaces would implement multiple interfaces specified. The first argument specifies ClassLoader class loader to generate dynamic proxy class.
  • static Object newProxyInstance (ClassLoader loader, Class [] interfaces, InvocationHandler h <?>): directly create a dynamic proxy object, the proxy object implementation class implements interfaces specified serial interface, each method will be executed proxy object Alternatively InvocationHandler invoke method performs object.

In fact, even after using the first method to generate dynamic proxy class, if the program needs to create objects through the proxy class, still need to pass a InvocationHandler object. That is, each proxy object generated by the system has an associated InvocationHandler objects.

Tip: The computer is "stupid", the reflection mode when the program is used to generate the interface proxy object specified dynamic range, to achieve these dynamic proxy object class implements one or more interfaces, the dynamic proxy object needs to implement one or more of All methods defined in the interface, but the question is: how the system knows how to implement these methods? This time the turn InvocationHandler objects debut - when performing a dynamic proxy object in the method, in fact, will be replaced invoke method calls InvocationHandler right.

Mr procedures may be employed as a dynamic proxy class, and then to create a proxy object by dynamic proxy class manner to generate a dynamic proxy object. The following code snippet:

// Create a InvocationHandler objects 
InvocationHandler Handler = new new MyInvocationHandler (...);
 // Use Proxy generate a dynamic proxy class proxyClass 
Class proxyClass = Proxy.getProxyClass (Foo. Class .getClassLoader (), new new Class [] {Foo. Class });
 // Get proxyC1ass class constructor parameter with a InvocationHandler 
the constructor ctor = proxyClass.getConstructor ( new new class [] {InvocationHandler. class });
 // the newInstance method call to create dynamic instances ctor 
Foo f = (Foo ) ctor.newInstance ( new new Object [] {} Handler);

The above code can be simplified to the following code:

// Create a InvocationHandler objects 
InvocationHandler Handler = new new MyInvokationHandler (...);
 // specified to directly generate a dynamic Proxy proxy object 
Foo F = (Foo) the Proxy.newProxyInstance (Foo. Class .getClassLoader (), new new Class [ ] {Foo. class }, Handler);

The following procedure demonstrates the use of Proxy and InvocationHandler to generate dynamic proxy object.

interface the Person {
     void Walk (); 

    void the sayHello (String name); 
} 

class MyInvokationHandler the implements of InvocationHandler {
     / * 
     * all the methods of performing dynamic proxy object, will be replaced to perform the invoke method wherein: proxy: Representative dynamic proxy objects method: the method is being performed on behalf of 
     * args: behalf of an incoming call when the target method arguments. 
     * / 
    Public Object Invoke (Object Proxy, Method, Method, Object [] args) { 
        System.out.println ( "---- method being performed:" + Method);
         IF (! Args = null ) { 
            the System.out .println ( "Here is passed when performing the method as arguments:" );
             for (Object Val: args) {
                System.out.println (Val); 
            } 
        } the else { 
            System.out.println ( "call the method takes no arguments!" ); 
        } 
        Return  null ; 
    } 
} 

public  class ProxyTest {
     public  static  void main (String [] args) throws Exception {
         // create a InvocationHandler objects 
        InvocationHandler Handler = new new MyInvokationHandler ();
         // specified InvocationHandler to generate a dynamic proxy object 
        the Person P = (the Person) the Proxy.newProxyInstance (the Person. class.getClassLoader (), new new Class [] {the Person. class }, 
                Handler); 
        // call to the proxy object moving walk () and sayHello () method 
        p.walk (); 
        p.sayHello ( "Monkey" ); 
    } 
}

Needs to be rewritten invoke () method above when the program is first provided a Person interface, which contains the walk () and sayHello () two abstract method, then a simple definition of InvocationHandler implementation class, which defines the implementation class - calling all proxy object methods are replaced by the call invoke () method. The invoke () method of the three parameters is explained below.

  • proxy: dynamic represents the proxy object.
  • method: method represents being executed.
  • args: an incoming call when the target method on behalf of arguments.

The first line in the above procedure bold InvocationHandler code creates an object, the second line of code creates a bold dynamic proxy object according InvocationHandler object. Run the above procedures, see the following operation effect as shown in FIG.

---- method being implemented: public  abstract  void com.jwen.chapter18_5.Person.walk () 
This method is called without arguments! 
---- method being implemented: public  abstract  void com.jwen.chapter18_5.Person.sayHello (java.lang.String) 
passed in when the method is to perform the following argument is: 
Monkey King

As can be seen from the chart, regardless of the program is to walk the implementation of the proxy object () method, or the implementation of the proxy object sayHello () method, you are actually executed invoke InvocationHandler object () method.

After reading the above sample program, Readers may feel that this program is not much practical value, it is difficult to understand Java dynamic proxy charm. In fact, in general programming, indeed without the use of dynamic proxies, but in the preparation of the framework or underlying code, dynamic proxy role is very big.

Dynamic proxy and AOP

According to Proxy and InvocationHandler described earlier, it is difficult to see the advantages of this dynamic proxies. Here's a more practical dynamic proxy mechanism.

The development of practical application of software systems, there are often cases the same code segment recurring, in this case, for many people the beginning engaged in software development, and their approach is: select those codes, all the way to "copy" "paste", immediately realized the system function, if only from the point of view of software functionality, they do have completed the software development.

By such a "copy" developed "paste" mode software as shown below.

As shown in the use of software architecture implemented on a map, during the software development may feel does not matter, but if there is to achieve a large dark code required to modify the program, it means three open source code modifications. If there is one place where even 1000 using this code snippet dark, then the modification, maintenance workload of the code will become a nightmare.

In this case, most with experienced developers will be dark this code segment is defined as a method, and then let the other three sections of code snippets which can be called directly. In this manner, the structure of the software system as shown in FIG.

 

For software system shown above, if you need to modify the code dark part, as long as a place where you can modify the code segment of the method is invoked, no matter how many places a call to this method, are completely without any modification, As long as the called method is modified, all local calls will naturally change the method - in this way greatly reduces the complexity of the latter part of software maintenance.

But this way to achieve code reuse is still generating an important question: Code segment 1, code segments 2, 3 and dark snippet of code segment points away, but the code segment 1, code snippets, and code segments 2 and 3 and a particular method couples! The best results are: a code block, code block 2 and 3. The method may be performed dark portion of code, but without hard-coded in the program to directly call the dark code block, then it may be dynamic proxies to achieve this effect.

Since JDK dynamic proxy can only create a dynamic proxy for the interface, so the following will provide a Dog interface that code is very simple, just define two methods in this interface.

public  interface Dog {
     // info method declaration 
    void info (); 

    // RUN method declaration 
    void RUN (); 
}

Above the interface was simply defines two methods, it does not provide a method to achieve. If the direct use Proxy interface to create a dynamic proxy object for, the dynamic proxy object implementation of the results of all methods in turn exactly the same. Is often the case, the software will interface provides one or more implementation classes for the Dog, to provide a simple implementation class here: GunDog.

public  class gundog the implements Dog {
     // implement info () method, only a print string 
    public  void info () { 
        System.out.println ( "I am a hunting dog" ); 
    } 

    // implement run () method, only Print a string 
    public  void RUN () { 
        System.out.println ( "I run fast" ); 
    } 
}

The above special code is not the slightest, the implementation class Dog only provides a simple implementation for each method. Look at the need to implement the function: make the code segment 1, code segments 2 and 3 can either execute code segments dark portion of code, and without hard coding method invocation dark code directly in the program. Here is assumed info (), run () method represents two code segment 1, segment 2, then requirements: info (), run () method of performing a program can call a general procedure, but do not want to hard-coded this method is called. The following provides a DogUtil class that contains two general methods.

public  class DogUtil {
     // first interceptor method 
    public  void the method1 () { 
        System.out.println ( "Simulation of a general method ===== =====" ); 
    } 

    // second interceptor method 
    public  void method2 () { 
        System.out.println ( "General method II ===== ===== analog" ); 
    } 
}

And by means of Proxy InvocationHandler can be achieved - when the program calls info () method and the run () method, the system may "automatically" to the method1 () and method2 () method to insert two general info () and run () method in execution.

The key to this procedure is that the following MyInvocationHandler class, which is a InvocationHandler implementation class, the implementation class Invoke () method will be implemented as a proxy object.

public  class MyInvokationHandler the implements of InvocationHandler {
     // needs to be proxy object 
    Private Object target; 

    public  void setTarget (Object target) {
         the this .target = target; 
    } 

    // the implementation of all Dynamic proxy object, will be replaced to perform the following invoke method 
    public Object invoke (Object Proxy, method, method, Object [] args) throws Exception { 
        DogUtil du = new new DogUtil ();
         // perform DogUtil object method1. 
        du.method1 ();
         // In performing the scheduling method as target method 
        Object result =Method.invoke (target, args);
         // perform DogUtil object method2. 
        du.method2 ();
         return Result; 
    } 
}

The above program implements invoke () method comprising a row when the key code (indicated in bold), this line of code to target as reflected by the scheduling method performing the method, which is the original callback method of the target object. DogUtil object before calling bold codes the method1 () method call DogUtil object after the code bold method2 () method.

The following provides a further class of MyProxyFactory program designed to generate the object dynamic proxy instance for the specified target.

public  class MyProxyFactory {
     // target to generate dynamic proxy object for the specified 
    public  static Object The getProxy (Object target) throws Exception {
         // create a MyInvokationHandler objects 
        MyInvokationHandler Handler = new new MyInvokationHandler ();
         // set the target object MyInvokationHandler 
        handler.setTarget (target );
         // create and return a dynamic proxy 
        return Proxy.newProxyInstance (target.getClass () getClassLoader (), target.getClass () getInterfaces (), Handler);.. 
    } 
}

The above dynamic proxy factory class provides a The getProxy () method, which generates a dynamic proxy object to the target object, the target object dynamic proxy implements then imports the same, and therefore have the same public methods - up in this sense look, dynamic proxy object can be used as target objects to use. When the program calls the specified method of dynamic proxy object, in fact, it becomes invoke () method to perform MyInvocationHandler object. For example, calling the dynamic proxy object info () method, execution will invoke () method, which performs the following steps.

  1. Creating DogUtil instance.
  2. method1 performed DogUtil instance () method.
  3. A reflective target to perform as the caller info () method.
  4. Examples of execution DogUtil method2 () method.

Both during program execution info (), run () method "Insert - When using dynamic proxy object instead of the target object, a proxy object methods to achieve the previous requirements: see the implementation of the above process, the reader should have been found "method1 (), method2 () general method, but the method GunDog in and no hard-coded calls method1 () and method2 () method.

Here are a main program to test the effect of this dynamic proxy.

public  class the Test {
     public  static  void main (String [] args) throws Exception {
         // create a gundog original object as target 
        Dog target = new new gundog ();
         // specified target to create a dynamic proxy 
        Dog Dog = ( Dog) MyProxyFactory.getProxy (target); 
        dog.info (); 
        dog.run (); 
    } 
}

dog objects above program is actually a dynamic proxy object, but the dynamic proxy object also implements the Dog interface, so you can also use as a Dog object. When the program execution dog info () and run () method, in fact, will first execute method1 DogUtil's () method, and then perform the target object info () and run () method, the final implementation of method2 DogUtil () method. Run the above procedures, you will see the results shown below in FIG.

===== ===== simulation of a generic method 
I'm a hound
 ===== ===== analog common method two 
===== ==== simulation of a general method = 
I run quickly
 ===== ===== analog common method two

By running the results shown above point of view, difficult to find a dynamic agent it can be very flexible decoupling. In general, when using a Proxy generate dynamic proxy, often does not happen by a dynamic proxy, so there is not much practical significance. Usually generate dynamic proxy for the specified target object.

This dynamic proxy AOP (Aspect Orient Programming, Oriented Programming) is referred AOP proxy, the proxy may be used instead of the target object AOP, AOP agent contains all the methods of the target object. But there are differences in methods of the target object AOP proxies: AOP proxy in the method may be executed before the target method, then insert some general-purpose processing.

AOP proxy method comprising a method of the target object included in the following diagram shown in FIG.

 

Guess you like

Origin www.cnblogs.com/jwen1994/p/12575284.html