?????????????????Client??
public class Client
{
public static void main(String[] args)
{
//    ?????????????????
Subject realSubject = new RealSubject();
//    ???????????????????????????????????????????????????????????
InvocationHandler handler = new DynamicProxy(realSubject);
/*
* ???Proxy??newProxyInstance?????????????????????????????????????????
* ????????? handler.getClass().getClassLoader() ?????????????handler??????ClassLoader?????????????????????
* ?????????realSubject.getClass().getInterfaces()?????????????????????????????????????е???????????????????????????????????????????е??????
* ??????????handler?? ?????????????????????????????? InvocationHandler ?????????
*/
Subject subject = (Subject)Proxy.newProxyInstance(handler.getClass().getClassLoader()?? realSubject
.getClass().getInterfaces()?? handler);
System.out.println(subject.getClass().getName());
subject.rent();
subject.hello("world");
}
}
????????????????????????????
????$Proxy0
????before rent house
????Method:public abstract void com.xiaoluo.dynamicproxy.Subject.rent()
????I want to rent my house
????after rent house
????before rent house
????Method:public abstract void com.xiaoluo.dynamicproxy.Subject.hello(java.lang.String)
????hello: world
????after rent house
?????????????????? $Proxy0 ?????????????????????????? System.out.println(subject.getClass().getName()); ????????????????????????????????????????????????????????
????Subject subject = (Subject)Proxy.newProxyInstance(handler.getClass().getClassLoader()?? realSubject
????.getClass().getInterfaces()?? handler);
???????????????????????????????Subject??????????????InvocationHandler?????????????????????????????????????????????????Subject???????????????newProxyInstance?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????е??????????????????????Subject???????????????????Subject???????
?????????????????????? Proxy.newProxyInstance ????????????????jvm?????????????????????????????????InvocationHandler????????????????????????????????????????????????????????????????????????????????????$?????proxy??У????????????????????
????????????????????????
????subject.rent();
????subject.hello("world");
?????????????????????????????????????е???????????????????????????????????????? handler ?е?invoke???????У??????????? handler ?????????????? RealSubject???????????????????????????????????????????? handler ?е?invoke???????У?
public Object invoke(Object object?? Method method?? Object[] args)
throws Throwable
{
//????????????????????????????Щ????????
System.out.println("before rent house");
System.out.println("Method:" + method);
//    ???????????????????????????????????????????????????handler?????invoke?????????е???
method.invoke(subject?? args);
//??????????????????????????????Щ????????
System.out.println("after rent house");
return null;
}
????????????????????????????????????????????????????????????÷???????????????Щ??????????????????????? method ?????????????
????public abstract void com.xiaoluo.dynamicproxy.Subject.rent()
????public abstract void com.xiaoluo.dynamicproxy.Subject.hello(java.lang.String)
???????????????Subject????е?????????????????????????????????????÷????????????????????????????? handler ?????invoke??????????????????????????????????????????????????????
?????????????java??????????
????????????????????java?е??????????????????????????????????????Spring??AOP????????????????????????????????????ú???????????????