Cglib dynamic proxy and cglib dynamic

Source: Internet
Author: User

Cglib dynamic proxy and cglib dynamic

The proxy adds a control layer for the access object to make it indirect. The control layer can add operation properties for the object access.

Cglib: Code Generation library,

  • High-performance code generation package based on ASM (java bytecode operation code)
  • Used by many AOP frameworks
  • Different from JDK dynamic proxy, cglib does not need to implement interfaces.

Instance:

 1  import java.lang.reflect.Method;   2     3  import net.sf.cglib.proxy.Enhancer;   4  import net.sf.cglib.proxy.MethodInterceptor;   5  import net.sf.cglib.proxy.MethodProxy;   6     7     8  public class MyMethodInterceptor implements MethodInterceptor {         9      10  11      public String myFun(String arg){  12          return "hello," + arg ;  13      }  14       15      public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable {  16          String methodName = method.getName();  17           18          System. out .println( "[intercept] fun invoked before" );  19          String result = (String)args[0] + "..." ;  20          System. out .println( result );  21          System. out .println( "[intercept] fun invoked after" );  22          return result;       23      }  24       25      public Object createProxy(){  26          Enhancer enhancer = new Enhancer();  27          enhancer.setSuperclass(MyMethodInterceptor. class );  28          enhancer.setCallback( this );  29          return enhancer.create();  30      }  31       32       33      public static void main(String[] args) {  34          MyMethodInterceptor ss = new MyMethodInterceptor();  35          MyMethodInterceptor proxy = (MyMethodInterceptor)ss.createProxy();  36 37          c1.myFun( "cglib test" );  38           39      }  40    41  }  

Common tools:

 1 package org.windwant.spring.core.proxy; 2  3 import org.springframework.cglib.proxy.Enhancer; 4 import org.springframework.cglib.proxy.MethodInterceptor; 5 import org.springframework.cglib.proxy.MethodProxy; 6  7 import java.lang.reflect.Method; 8  9 /**10  * Created by windwant on 2016/6/4.11  */12 public class MyCGLIBProxy implements MethodInterceptor {13     private Enhancer enhancer = new Enhancer();14     public Object getProxy(Class clazz){15         enhancer.setSuperclass(clazz);16         enhancer.setCallback(this);17         return enhancer.create();18     }19 20     public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {21         System.out.println("cglib before action");22         Object result = methodProxy.invokeSuper(o, objects);23         System.out.println("cglib after action");24         return result;25     }26 }
1 MyCGLIBProxy p = new MyCGLIBProxy();2 Performer pp = (Performer) p.getProxy(XXXX.class);3 pp.XXXX();

 

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.