Java dynamic proxy dynamic proxy implementation and practical application

First, the concept

1, there is a proxy object value is mainly used to block access to real business objects.
2, and the proxy object should have a target audience the same way (real business objects).

Two, Java dynamic proxy implementation

1. "java.lang.reflect.Proxy" category Introduction

  To generate a proxy object to an object, the proxy object usually have to write a class to generate , so the first to be prepared for generating a proxy object of the class. How to generate an object program in java proxy object it, java provides a "java.lang.reflect.Proxy" category after JDK1.5, a newProxyInstance method "Proxy" class offered to create an object the proxy object.

static Object newProxyInstance(ClassLoader loader, Class<?>[] interfaces, InvocationHandler h)

Parameter Description:

ClassLoader loader to generate a proxy object for indicating which class loader; 
Class <?> [] To the interfaces specified proxy object which object is generated, designated by the interface; 
of InvocationHandler H generated for indicating the proxy object to do something.

2. Write generate the proxy object class

  In java provisions, in order to generate the proxy for an object, the object must have an interface. Interface is defined as follows:

Package com.javaBase.Proxy; 

/ ** 
 * <person interfaces>; 
 * <detailed description Function> 
 * 
 * @author 
 * @see [Class / Method] (optional) 
 * @Since [Product / module versions] (available option)
  * / 
public  interface PersonService { 

    / ** 
     * description: 
     * <eat> 
     * 
     * @see [class / method] (optional) 
     * @Since [product / module versions] (optional)
      * / 
    public  void EAT (); 

    / ** 
     * description: 
     * <sleep> 
     * 
     * @see [class / method] (optional) 
     *@Since [Product / module versions] (optional)
      * / 
    public  void SLEEP (); 
}

Interface:

Package com.javaBase.Proxy; 

/ ** 
 * <Function Description word>; 
 * <Ma> 
 * 
 * @author JXX 
 * @see [Class / Method] (optional) 
 * @Since [Product / module versions] (optional)
  * / 
public  class MrMaImpl the implements PersonService { 

    @Override 
    public  void eAT () { 
        System.out.println ( "Ma eating ..." ); 
    } 

    @Override 
    public  void SLEEP () { 
        System.out.println ( "doze Ma ..." ); 
    } 
}

Construction of proxy method and test code:

Package com.javaBase.Proxy; 

Import java.lang.reflect.InvocationHandler;
 Import the java.lang.reflect.Method;
 Import the java.lang.reflect.Proxy; 

/ ** 
 * <Function Description word>; 
 * <Function Details description> 
 * 
 * @author JXX 
 * @see [class / method] (optional) 
 * @Since [product / module versions] (optional)
  * / 
public  class MrMaProxy { 

    Private PersonService mayun = new new MrMaImpl (); 

    / * * 
     * Get method of the proxy object 
     * @return 
     * / 
    public PersonService the getProxy () {
        return (PersonService) Proxy.newProxyInstance (MrMaImpl. class .getClassLoader (), mayun.getClass (). getInterfaces (), new new InvocationHandler () {
             / ** 
             * 
             * @param Proxy proxy object itself 
             * @param Method, the proxy object for the current call method 
             * @param args parameter method 
             * @return 
             * @throws the Throwable
              * / 
            @Override 
            public Object Invoke (Object Proxy, method, method, Object [] args) throws the Throwable {
                 IF("eat".equals(method.getName())) {
                    System.out.println("开始吃饭");
                    method.invoke(mayun,args);
                } else if("sleep".equals(method.getName())) {
                    System.out.println("开始睡觉");
                    method.invoke(mayun,args);
                }
                return null;
            }
        });
    }

    public static void main(String[] args){
        MrMaProxy proxy = new MrMaProxy();
        PersonService ps = proxy.getProxy();
        ps.eat();
        ps.sleep();
    }
}

operation result:

Began to eat 
Ma at dinner ... 
I went to sleep 
Ma nap ...

Third, the dynamic proxy application

  invoke methods in a dynamic agent technology, due no matter what method the user invokes the proxy object, call processors are written developer (which is equivalent to invoke methods to intercept the proxy object method call). In addition, developers invoke parameter method, you can also intercept at the same time, knowing what method is invoked by the user, so the use of these two features, you can achieve some special needs, such as: intercept a user's access request, in order to check the user is there access to dynamically add additional functionality to an object. Practical applications are as follows:

  • Spring AOP dynamic proxy implementation. Spring AOP dynamic proxy implementation There are two main ways, JDK dynamic proxies and CGLIB bytecode generation. By default, if Spring AOP find the target object implements the appropriate interface, it uses JDK dynamic proxy mechanism for generating a proxy object. If the interface is not found, CGLIB manner is used to generate dynamic proxy object instance as the target object.
  • RPC application framework. RPC framework is actually a problem to be solved is: how to call a remote service to others? Like calling a local call services as remote service. How to encapsulate data, transmitted over the network to a remote service, the remote interface transparent, which requires dynamic proxies help. Therefore, transparency is to utilize a remote service call dynamic agent, encapsulating the data, network transmission, etc. operate in the proxy layer.

 

 

Reference links: the Java dynamic proxy implementation and practical application

     Dynamic proxy principle and its application

Guess you like

Origin www.cnblogs.com/jxxblogs/p/12050944.html