关闭

jdk、cglib动态代理

标签: cglibjdk动态代理
100人阅读 评论(0) 收藏 举报
分类:

JAVA 动态代理模式

Jdk动态代理

仅支持对实现了同一接口的类进行代理

 

创建一个被代理类的接口:

public interface Moveable {

       void move();

       void stop();

}

 

创建被代理类接口的实现

public class Car implements Moveable {

       @Override

       public void move() {

              System.out.println("汽车行驶中...");

       }

       @Override

       public void stop() {

              System.out.println("汽车停止行驶...");

       }

}

 

创建日志功能的代理类

public class ProxyHandler implements InvocationHandler{

       private Object obj;

       public ProxyHandler(Object obj){

              this.obj = obj;

       }

       @Override

       public Object invoke(Object proxy, Method method,Object[] args)

                     throws Throwable {

             

              System.out.println("日志开始...");

              Objecto = method.invoke(obj, args);

              System.out.println("日志结束...");

              return o;

       }

}

 

创建测试

public class Test {

       public staticvoid main(String[] args) {

              Carcar = newCar();

              Class<?>[]interfaces = car.getClass().getInterfaces(); 

              Moveablem=(Moveable) Proxy.newProxyInstance(Moveable.class.getClassLoader(),

                            interfaces,newProxyHandler(car));

              //m.move();

              m.stop();

       }

}

 

输出内容

测试成功

 

Cglib动态代理

能对任意类的任意方法实现代理,没有jdk动态代理的 必须实现同一接口的限制

需引入cglib.jar

 

创建被代理类

public class Train {

       public voidmove(){

              System.out.println("火车行驶中...");

       }

}

 

创建cglib日志代理类

import java.lang.reflect.Method;

import net.sf.cglib.proxy.Enhancer;

import net.sf.cglib.proxy.MethodInterceptor;

import net.sf.cglib.proxy.MethodProxy;

 

public class CglibProxy implements MethodInterceptor{

       private Object object;

       public CglibProxy(Object object){

              this.object= object;

       }

      

       public Object Proxy(){

              //创建加强器

              Enhancerenhancer = newEnhancer();

              //设置需加强的类

              enhancer.setSuperclass(object.getClass());

              //设置回调

              enhancer.setCallback(this);

              enhancer.setClassLoader(object.getClass().getClassLoader());

              return enhancer.create();

       }

       @Override

       public Object intercept(Object arg0, Method arg1,Object[] arg2,

                     MethodProxyarg3) throwsThrowable {

              //对被代理类的加强

              System.out.println("日志开始...");

              Objecto = arg3.invokeSuper(arg0, arg2);

              System.out.println("日志结束...");

              return o;

       }

      

}

 

创建测试类

public class Test {

       public staticvoid main(String[] args) {

              CglibProxycglibProxy = newCglibProxy(new Train());

              TrainnewTrain = (Train) cglibProxy.Proxy();

              newTrain.move();

       }

}

 

输出内容

测试成功


0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:3366次
    • 积分:128
    • 等级:
    • 排名:千里之外
    • 原创:5篇
    • 转载:22篇
    • 译文:0篇
    • 评论:0条