????????Proxy?????У?Sun????????JDK1.3?????Java??????????????????????????????????????????е?????????? java.lang.reflect.Proxy???????????????????壬????Java???書???????????? java.lang.reflect.InvocationHandler???????????????????????????2????е??????Handler??? InvocationHandler?????????Handler??????????????????????Handler?????????Handler?????? invoke???????÷??????????Java?????????????????????????????????????????????????????????????????Log????Proxy?????Handler??????????????????????????????????????????????????????????????????????????????Handler?? invoke??????

????Proxy?????????????????????????????????
??????1??getProxyClass()?????????????????????????????????????£?
????public static Class<?> getProxyClass(ClassLoader loader?? Class<?>[] interfaces) throws IllegalArgumentException
????????loader ??????????????????????????interfaces ??????????????????????н???
??????2??newProxyInstance()???????????????????????????????????????????£?
????public static Object newProxyInstance(ClassLoader loader?? Class<?>[] interfaces?? InvocationHandler handler) throws
????IllegalArgumentException
????????loader ??????????????????????????interfaces ??????????????????????н???????handler ????????????????? InvocationHandler ????
?????????????????????????Foo??????????????????
????/**** ???? ****/
????//????InvocationHandler????
????InvocationHandler handler = new MyInvocationHandler(...);
????//?????????????
????Class proxyClass = Proxy.getProxyClass(Foo.class.getClassLoader()?? new Class[] { Foo.class });
????//?????????????????
????Foo foo = (Foo) proxyClass.getConstructor(new Class[] { InvocationHandler.class }).
????newInstance(new Object[] { handler });
????/**** ????? ****/
????//????InvocationHandler????
????InvocationHandler handler = new MyInvocationHandler(...);
????//???????????????????
????Foo foo = (Foo) Proxy.newProxyInstance(Foo.class.getClassLoader()??new Class[] { Foo.class }?? handler);
??????Proxy?????????????????????????????????
???????????????public??final????????????
?????????????????java.lang.reflect.Proxy??
????????????????????“$Proxy”?????
????????????????getProxyClass()??newProxyInstance()?????в???interfaces????????н???
????Proxy ???isProxyClass(Class<?> cl)??????????????ж??????????????????????????????Proxy???????????????????
????????????????????public ?????????????ù??????????InvocationHandler ??????????
??????Proxy??????????????????????????????????????
????1. ???????foo ?????????????????????????????????????????Foo ???????“foo instanceof Foo”????true???????foo???????Foo???????????
????(Foo) foo //???
????2.??????????????????????InvocationHandler ?????????Proxy ???getInvocationHandler(Object proxy)????????????????proxy?????????????????????InvocationHandler ????
????3.???Foo????????amethod()?????????????????????????????foo??amethod()????????÷??????????????????InvocationHandler ?????invoke()??????
????InvocationHandler ???????????y??????????????????????????????invoke()??????
????Object invoke(Object proxy??Method method??Object[] args) throws Throwable
????????proxy?????????????????????method???????????????????args ????????÷?????????????invoke()??????????????????÷???????????
???????????????????????spring ??AOP????
package proxy;
import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
import java.lang.reflect.Proxy;
class Logic{
public void logic(){
Dao dao = Factory.create();
System.out.println("dynamic proxy's name: "+dao.getClass().getName());
dao.insert();
}
}
class Factory{
static Dao create(){
Dao dao = new JdbcDao();
MyInvocationHandler hand = new MyInvocationHandler();
return (Dao)hand.get(dao);
}
}
interface Dao{
public void update();
public void insert();
}
class JdbcDao implements Dao{
public void update(){
System.out.println("in jdbc update");
}
public void insert(){
System.out.println("in jdbc insert");
}
}
class HibernateDao implements Dao{
public void update(){
System.out.println("in hibernate update");
}
public void insert(){
System.out.println("in hibernate insert");
}
}
class MyInvocationHandler implements InvocationHandler {
Object o;
public Object get(Object o){
System.out.println("in get method of MyInvocationHandler");
this.o = o;
return Proxy.newProxyInstance(o.getClass().getClassLoader()??o.getClass().getInterfaces()??this);
}
public Object invoke(Object arg0?? Method arg1?? Object[] arg2)
throws Throwable {
System.out.println("write log before invoke");
Object result = arg1.invoke(o?? arg2);
System.out.println("write log after invoke");
return result;
}
}
public class Test {
public static void main(String[] args) {
Logic l = new Logic();
l.logic();
}
}
???????н??:
????in get method of MyInvocationHandler
????dynamic proxy's name: proxy.$Proxy0
????write log before invoke
????in jdbc insert
????write log after invoke
????????: JDK?????????????????????????????е???Proxy.newProxyInstance????????????????????????飬 ???Proxy?????????