动态代理

Java世界的动态代理有java的jdk动态代理和cglib动态代理。区别主要是jdk是被代理类必须实现接口,而cglib则不需要,cglib是生成代理类子类,在子类调用被代理类(父类)接口时进行拦截。

 

1. java动态代理

被代理类

//被代理类实现接口
public
interface Moveable { void move(); } //被代理类 public class Car implements Moveable { public void move() { System.out.println("the car begin to move"); } }

代理类:

 


import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method
public class JdkProxy implements InvocationHandler {
    private Object target;

    public JdkProxy(Object target) {
        this.target = target;
    }
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
        System.out.println("before run :");
        method.invoke(target);
        System.out.println("after run");
        return null;
    }
}

Test:



import java.lang.reflect.Proxy;

public class Test {
public static void main(String[] args) {
/**
* jdk dynamic proxy
*/
Moveable car=new Car();
JdkProxy jdkProxy=new JdkProxy(car);
Moveable moveable=(Moveable) Proxy.newProxyInstance(timeHandler.getClass().getClassLoader(),car.getClass().getInterfaces(),jdkProxy);
moveable.move();
}
}


//测试结果

  before run :
  the car begin to move
  after run



 

2. cglib动态代理

被代理类:

import com.djtu.vwater.security.AdminOnly;
import org.springframework.stereotype.Service;

@Service
public class TrainService {
    public void move(){
        System.out.println("train move");
    }

}

代理类:

 

import org.springframework.cglib.proxy.Enhancer;
import org.springframework.cglib.proxy.MethodInterceptor;
import org.springframework.cglib.proxy.MethodProxy;
import java.lang.reflect.Method;

public class CglibProxy implements MethodInterceptor {
    private Object targetObject;
    Enhancer enhancer=new Enhancer();

    public Object getProcy(Object clazz) {
        targetObject=clazz;
        enhancer.setSuperclass(clazz.getClass());
        enhancer.setCallback(this);
        return enhancer.create();
    }


    /**
     *
     * @param o 目标类的实例
     * @param method 目标方法的反射对象
     * @param objects 方法参数
     * @param methodProxy 代理类的实例
     * @return
     * @throws Throwable
     */
    public Object intercept(Object o, Method method, Object[] objects, MethodProxy methodProxy) throws Throwable {
        System.out.println("before move:");
        methodProxy.invoke(targetObject,objects);
        methodProxy.invokeSuper(o,objects);
        System.out.println("after move");
        return null;
    }
}

Test:

import com.djtu.vwater.cglib.CglibProxy;
import com.djtu.vwater.cglib.TrainService;

import java.lang.reflect.Proxy;

public class Test {
    public static void main(String[] args)  {
//        /**
//         * jdk dynamic proxy
//         */
//        Moveable car=new Car();
//        JdkProxy jdkProxy=new JdkProxy(car);
//        Moveable moveable=(Moveable) Proxy.newProxyInstance(jdkProxy.getClass().getClassLoader(),car.getClass().getInterfaces(),jdkProxy);
//        moveable.move();

        /**
         * cglib dynamic proxy
         */

        CglibProxy timeHandler=new CglibProxy();
        TrainService trainService =(TrainService) timeHandler.getProcy(new TrainService());
        trainService.move();
    }
}

//测试结果

before move:
train move
train move
after move

 

 

posted @ 2019-09-04 14:54  vwater  阅读(94)  评论(0编辑  收藏  举报