Java中两种动态代理的实现

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

1、JDK实现

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

  • 接口Work.java
public interface Work {
    public void work();
}
  • 实现类WorkImpl.java
public class WorkImpl implements Work {

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

}
  • 一个添加日志的代理类LogHandler.java,这里用到了log4j。
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;
    }

}
  • 最后测试类Test.java
public class Test {
    public static void main(String[] args) {
        Work w = new WorkImpl();
        w.work();
        Work logWork =  new LogHandler<Work>(w).getInstance();
        logWork.work();
    }
}

 

2、CGLIB实现

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

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

  • 业务相关类WorkImpl.java
public class WorkImpl   {

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

}
  • CGLIB动态代理类LogHandler.java,同样是添加日志信息
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;
    }
    
}
  • 测试类Test.java
public class Test {
    public static void main(String[] args) {
        WorkImpl w = new WorkImpl();
        w.work();
        WorkImpl logWork = new LogHandler<WorkImpl>(w).getInstance();
        logWork.work();
    }
}

 

posted @ 2014-05-09 10:38  废纸菩提  阅读(184)  评论(0编辑  收藏  举报