Java中两种动态代理的实现

时间:2014-05-09 13:29:15   收藏:0   阅读:379

本文介绍了java中两种动态代理的实现方法,Spring的动态代理也是基于这两种方法的。直接附上源码:

1、JDK实现

使用JDK实现动态代理必须使用接口

bubuko.com,布布扣
public interface Work {
    public void work();
}
bubuko.com,布布扣
bubuko.com,布布扣
public class WorkImpl implements Work {

    @Override
    public void work() {
        System.out.println("我在工作");
    }

}
bubuko.com,布布扣
bubuko.com,布布扣
public class LogHandler<T> implements InvocationHandler{
    
    public T target;
    
    private static Logger logger = Logger.getLogger(LogHandler.class);
    
    public LogHandler(T target){
        this.target = target;
    }
    
    @SuppressWarnings("unchecked")
    public T getInstance(){
        return (T) Proxy.newProxyInstance(target.getClass().getClassLoader(), target.getClass().getInterfaces(), this);
    }
    
    @Override
    public Object invoke(Object proxy, Method method, Object[] args)
            throws Throwable {
        logger.warn("start");
        Object result = method.invoke(target, args);
        logger.warn("end");
        return result;
    }

}
bubuko.com,布布扣
bubuko.com,布布扣
public class Test {
    public static void main(String[] args) {
        Work w = new WorkImpl();
        w.work();
        Work logWork =  new LogHandler<Work>(w).getInstance();
        logWork.work();
    }
}
bubuko.com,布布扣

 

2、CGLIB实现

CGLIB实现动态代理是对JDK动态代理的一种补充,可以不实现接口就能动态代理。

首先需要引入CGLIB的相关jar包(cglib-nodep-2.2.2.jar)

bubuko.com,布布扣
public class WorkImpl   {

    public void work() {
        System.out.println("我在工作");
    }

}
bubuko.com,布布扣
bubuko.com,布布扣
public class LogHandler<T> implements MethodInterceptor{
    
    public T target;
    
    private static Logger logger = Logger.getLogger(LogHandler.class);
    
    public LogHandler(T target){
        this.target = target;
    }
    
    @SuppressWarnings("unchecked")
    public T getInstance(){
        //使用CGLIB生成子类,并委托代理对象代理
        return (T) Enhancer.create(target.getClass(), this);
    }
    
    @Override
    public Object intercept(Object obj, Method method, Object[] args,
            MethodProxy proxy) throws Throwable {
        logger.warn("start!");
        Object result=proxy.invokeSuper(obj, args);
        logger.warn("end!");
        return result;
    }
    
}
bubuko.com,布布扣
bubuko.com,布布扣
public class Test {
    public static void main(String[] args) {
        WorkImpl w = new WorkImpl();
        w.work();
        WorkImpl logWork = new LogHandler<WorkImpl>(w).getInstance();
        logWork.work();
    }
}
bubuko.com,布布扣

 

Java中两种动态代理的实现,布布扣,bubuko.com

评论(0
© 2014 mamicode.com 版权所有 京ICP备13008772号-2  联系我们:gaon5@hotmail.com
迷上了代码!