本文介绍了java中两种动态代理的实现方法,Spring的动态代理也是基于这两种方法的。直接附上源码:
1、JDK实现
使用JDK实现动态代理必须使用接口
public interface Work { public void work(); }
public class WorkImpl implements Work { @Override public void work() { System.out.println("我在工作"); } }
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; } }
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)
public class WorkImpl { public void work() { System.out.println("我在工作"); } }
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; } }
public class Test { public static void main(String[] args) { WorkImpl w = new WorkImpl(); w.work(); WorkImpl logWork = new LogHandler<WorkImpl>(w).getInstance(); logWork.work(); } }
原文:http://www.cnblogs.com/sunxueqiang0329/p/3718383.html