使用JDK动态代理 被代理的类 ,必须要实现接口。
PersonDao.java
package com.zf.proxy; public interface PersonDao { public void add(); public void update(); }
package com.zf.proxy; public class PersonDaoImpl implements PersonDao { @Override public void add() { System.out.println("personDao add()..."); } @Override public void update() { System.out.println("personDao update()..."); } }
package com.zf.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class ProxyFactory<E> implements InvocationHandler{ private E target ; public ProxyFactory(E target){ this.target = target ; } @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("before..."); Object obj = method.invoke(target, args); System.out.println("after..."); return obj ; } public E getProxyInstance(){ return (E)Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(), this); } }
Test.java
package com.zf.proxy; public class Test { public static void main(String[] args) { PersonDao personDao = new PersonDaoImpl(); //泛型 传递接口类型进去 ProxyFactory<PersonDao> pf = new ProxyFactory<PersonDao>(personDao); personDao = pf.getProxyInstance(); personDao.update(); } }
代理链条的实现。
package com.zf.proxy; public interface BaseProxy<E> { E getProxyInstance(); void setTarget(E target); }
package com.zf.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class TimeProxyFactory<E> implements InvocationHandler , BaseProxy<E>{ private E target ; @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { System.out.println("log:" + target.getClass().getName() + "." + method.getName() + " start..."); Object obj = method.invoke(target, args); System.out.println("log:" + target.getClass().getName() + "." + method.getName() + " end..."); return obj ; } public E getProxyInstance(){ return (E)Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(), this); } @Override public void setTarget(E target) { this.target = target ; } }
package com.zf.proxy; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.Proxy; public class LogProxyFactory<E> implements InvocationHandler, BaseProxy<E>{ private E target ; @Override public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { long start = System.currentTimeMillis(); Object obj = method.invoke(target, args); long end = System.currentTimeMillis(); System.out.printf("方法%s用时:%d\n" , target.getClass().getName() + "." + method.getName() , (end - start)); return obj ; } //这里会将代理对象转换为E的类型,所以E应该为接口类型。而不能是接口的实现类。 public E getProxyInstance(){ return (E)Proxy.newProxyInstance(target.getClass().getClassLoader(),target.getClass().getInterfaces(), this); } @Override public void setTarget(E target) { this.target = target ; } }
package com.zf.proxy; public class ProxyChain<E> { private E target ; public ProxyChain(E target){ this.target = target ; } public void addProxy(BaseProxy<E> bp){ bp.setTarget(target); target = bp.getProxyInstance(); } public E getProxyInstance() { return target; } }
package com.zf.proxy; public class Test { public static void main(String[] args) { PersonDao personDao = new PersonDaoImpl(); //泛型 传递接口类型进去 ProxyChain<PersonDao> pc = new ProxyChain<PersonDao>(personDao); pc.addProxy(new TimeProxyFactory<PersonDao>()); pc.addProxy(new LogProxyFactory<PersonDao>()); personDao = pc.getProxyInstance(); personDao.update(); } }