Home  >  Article  >  Java  >  Detailed explanation of JDK and cglib code

Detailed explanation of JDK and cglib code

Y2J
Y2JOriginal
2017-05-10 10:25:141491browse

This article mainly introduces the dynamic proxy in java. Here is the relevant information about JDK dynamic proxy and cglib dynamic proxy

Detailed explanation of java dynamic proxy instance

1.jdk dynamic proxy

/** 
 * 
 */ 
package com.sinosoft; 
 
/** 
 *接口:编写一个委托类的接口,即静态代理的(Apple接口) 
 * 
 */ 
public interface Apple { 
   
  public void phoneCall(); 
} 

/** 
 * 
 */ 
package com.sinosoft; 
 
/** 
 * 实现一个真正的委托类,即静态代理的(AppleImpl类) 
 * 
 */ 
public class AppleImpl implements Apple { 
 
  /* 
   * 打电话 
  */ 
  @Override 
  public void phoneCall() { 
    System.out.println("打电话"); 
  } 
 
} 

/** 
 * 
 */ 
package com.sinosoft; 
 
import java.lang.reflect.InvocationHandler; 
import java.lang.reflect.Method; 
 
/** 
 * 创建一个动态代理类,实现InvocationHandler接口,并重写该invoke方法 
 * 
 */ 
public class DynamicProxy implements InvocationHandler{ 
  
   
  private Object object; 
   
  public DynamicProxy(Object object) { 
    this.object=object; 
  } 
  /* 
   * proxy参数传递的即是代理类的实例。method是调用的方法,即需要执行的方法;args是方法的参数; 
   * @param proxy 
   * @param method 
   * @param args 
   * @return 
   * @throws Throwable 
  */ 
  @Override 
  public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { 
    Object result = method.invoke(object, args); 
    return result; 
  } 
 
} 

/** 
 * 
 */ 
package com.sinosoft; 
 
import java.lang.reflect.Proxy; 
 
/** 
 * @author jdk动态代理 
 * 
 */ 
public class testDynamicProxy { 
  public static void main(String[] args) { 
    //1.创建接口的实现类 
    Apple tApple = new AppleImpl(); 
    //2.动态代理类 
    DynamicProxy tDynamicProxy = new DynamicProxy(tApple); 
    ClassLoader tClassLoader = tApple.getClass().getClassLoader(); 
    //   创建动态代理的对象,需要借助Proxy.newProxyInstance。该方法的三个参数分别是: 
    //   ClassLoader loader表示当前使用到的appClassloader。 
    //   Class<?>[] interfaces表示目标对象实现的一组接口。 
    //   InvocationHandler h表示当前的InvocationHandler实现实例对象。 
    Apple apple = (Apple) Proxy.newProxyInstance(tClassLoader, new Class[] { Apple.class }, tDynamicProxy); 
    apple.phoneCall(); 
  } 
}

2.cglib dynamic proxy

/** 
 * 
 */ 
package com.sinosoft; 
 
/** 
 * 实现一个真正的委托类,即静态代理的(AppleImpl类) 
 * 
 */ 
public class AppleClass{ 
  /* 
   * 打电话 
  */ 
  public void phoneCall() { 
    System.out.println("打电话"); 
  } 
 
} 

/** 
 * 
 */ 
package com.sinosoft; 
 
import java.lang.reflect.Method; 
 
import net.sf.cglib.proxy.MethodInterceptor; 
import net.sf.cglib.proxy.MethodProxy; 
 
/** 
 * @author Administrator 
 * 
 */ 
public class CglibProxy implements MethodInterceptor{ 
 
  /* 
   * 方法功能描述 
   * @param obj 
   * @param method 
   * @param args 
   * @param proxy 
   * @return 
   * @throws Throwable 
   * @see net.sf.cglib.proxy.MethodInterceptor#intercept(java.lang.Object, java.lang.reflect.Method, java.lang.Object[], net.sf.cglib.proxy.MethodProxy) 
  */ 
  @Override 
  public Object intercept(Object obj, Method method, Object[] args, MethodProxy proxy) throws Throwable { 
    // TODO Auto-generated method stub 
    Object object= proxy.invokeSuper(obj, args); 
    return object; 
  } 
 
} 

/** 
 * 
 */ 
package com.sinosoft; 
 
import net.sf.cglib.proxy.Enhancer; 
 
/** 
 * @author Administrator 
 * 
 */ 
public class TestCglibProxy { 
 
  public static void main(String[] args) { 
    CglibProxy tCglibProxy=new CglibProxy(); 
    Enhancer tEnhancer=new Enhancer(); 
    tEnhancer.setSuperclass(AppleClass.class);  
    tEnhancer.setCallback(tCglibProxy); 
     
    AppleClass tApple= (AppleClass)tEnhancer.create(); 
    tApple.phoneCall(); 
  } 
}

【Related recommendations】

1. JavaFree Video Tutorial

2. Java Video Tutorial on Implementing Equal-proportion Thumbnails of Images

3. Alibaba Java Development Manual

The above is the detailed content of Detailed explanation of JDK and cglib code. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn