Programming Optimization - Dynamic Agent (2) of the proxy mode

  Description: dynamic proxy means at run time dynamically generated proxy class. That proxy class bytecode will be generated and the current ClassLoader loaded at runtime. Compared with the static proxy class, there are many benefits. No need to write a formal package exactly the same class as the real theme, if too many interfaces, it will be a lot of proxy interface, the interface is slightly changed, the proxy interface should be changed; secondly, the use of dynamic proxy may specify the proxy class at runtime the execution logic, thereby enhancing the flexibility of the system.

  Agents have dynamic J DK own dynamic proxy, CGLIB, Javassist or ASM library . JDK dynamic proxy simple, built-in JDK, so no need to introduce third-party jar package, but the function is weak. CGLIB and Javassist are senior bytecode generation library that comes with the overall performance is better than JDK, and powerful. This introduces the use of CGLIB

 

1. The introduction of cglib maven project configuration in the pom.xml

    <dependency>
        <groupId>cglib</groupId>
        <artifactId>cglib</artifactId>
        <version>2.2.2</version>
    </dependency>

 

 2. The theme of the interface

    package com.hxc.proxy.proxyMode;
    
    /**
     * <P> Description: Theme Interface </ p>
     * @Author Huang Xiaocong
     * @Data 2019 Nian 10 Yue 27 Ri
     * / 
    Public  interface IDBQuery {
        String request();
    }

 

3. True topics

    package com.hxc.proxy.proxyMode;
    /**
     * <P> Description: real theme </ p>
     * @Author Huang Xiaocong
     * @Data 2019 Nian 10 Yue 27 Ri
     */
    public class DBQuery implements IDBQuery {
    
        public DBQuery() {
            try {
                Thread.sleep ( 1000 );     // simulation execution 
            } the catch (InterruptedException E) {
                e.printStackTrace ();
            }
        }
        public String request() {
            return "request String";
        }
    
    }

 

4. The proxy class CGLIB proxy class implements MethodInterceptor Interface (JDK dynamic proxy implement carrying InvocationHandler interface)

package com.hxc.proxy.proxyMode;

import java.lang.reflect.Method;

import net.sf.cglib.proxy.MethodInterceptor;
import net.sf.cglib.proxy.MethodProxy;

/**
 * <P> Description: proxy class </ p>
 * @Author Huang Xiaocong
 * @Data 2019 Nian 10 Yue 30 Ri
 */
public class CglibDbQueryInterceptor implements MethodInterceptor {

    private IDBQuery real = null;
    public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {
        if(real == null) {
            real = new DBQuery();
        }
        return real;
    }

}

 

5.Main, to complete the work and themes proxy class interfaces

  Create a proxy object steps:

  1) Logical specified cut, a proxy class definitions

  2) specified interface implemented

  3) Examples of generated proxy class

    package com.hxc.proxy.proxyMode;
import net.sf.cglib.proxy.Enhancer;

/**
     * <P> Description: Client </ p>
     * @Author Huang Xiaocong
     * @Data 2019 Nian 10 Yue 27 Ri
     * / 
    Public  class Main {
        
        public static void main(String[] args) {
            IDBQuery dbQuery = createCglibProxy();
            String request = dbQuery.request();
            System.out.println(request);
        }
        
        public static IDBQuery createCglibProxy() {
            Enhancer enhance = new Enhancer();
            enhance.setCallback ( new new CglibDbQueryInterceptor ());      // the specified cut, a logical proxy class defines
             // specified interface implemented 
            enhance.setInterfaces ( new new Class [] {IDBQuery. class });
             // generated proxy class instance 
            IDBQuery Object = (IDBQuery) enhance.create ();
             return  Object ;
        }
    
    }

 

  The most classic of dynamic proxies Hibernate application example is the use of proxy mode: delay loading One attribute; the second is delayed load the associated table.

 

Guess you like

Origin www.cnblogs.com/sun-flower1314/p/11748056.html