Java??????????????
?????xiaoluo501395377 ???????[ 2016/7/19 10:30:25 ] ??????????????????? Java
????????Spring????????????Spring?????????????????IoC?????????AOP??????IoC??????????????????????Spring?????AOP??????????????????????AOP????????????????????????????????????????????????????AOP???????java????????????????????????java??????????????????
??????java????????????У??????????????????????? InvocationHandler(Interface)??????????? Proxy(Class)???????????????????????????????????????????????????????java??API??????????????????????????????????
????InvocationHandler:
????InvocationHandler is the interface implemented by the invocation handler of a proxy instance. Each proxy instance has an associated invocation handler. When a method is invoked on a proxy instance?? the method invocation is encoded and dispatched to the invoke method of its invocation handler.
?????????????????????????InvocationHandler?????????????????????????????????????handler????????????????????????????????????????????????????InvocationHandler??????? invoke ?????????е??á???????????InvocationHandler?????????????? invoke ??????
????Object invoke(Object proxy?? Method method?? Object[] args) throws Throwable
???????????????????????????????????????????????????????????
????Object invoke(Object proxy?? Method method?? Object[] args) throws Throwable
????proxy:????????????????????????????
????method:???????????????????????????????????????Method????
????args:?????????????????????????????????????
????????????????????????????????????????????и???????
????????????????????Proxy?????
????Proxy provides static methods for creating dynamic proxy classes and instances?? and it is also the superclass of all dynamic proxy classes created by those methods.
????Proxy?????????????????????????????????????????????????????????????????? newProxyInstance ?????????
????public static Object newProxyInstance(ClassLoader loader?? Class<?>[] interfaces?? InvocationHandler h) throws IllegalArgumentException
????Returns an instance of a proxy class for the specified interfaces that dispatches method invocations to the specified invocation handler.
???????????????????????????????????????????????????????????????????????????????壺
????public static Object newProxyInstance(ClassLoader loader?? Class<?>[] interfaces?? InvocationHandler h) throws IllegalArgumentException
????loader:???????ClassLoader???????????????ClassLoader?????????????????????м???
????interfaces:???????Interface????????飬??????????????????????????????????????????????????????????????????????????????y??(???)????????????????????е??????
????h:???????InvocationHandler???????????????????????????????÷????????????????????InvocationHandler??????
?????????????????????????(??)??????????????????????????????????????????????
????????????????????Subject???????????????????????????
????public interface Subject
????{
????public void rent();
????public void hello(String str);
????}
???????????????????????????????????????????????????RealSubject??
????public class RealSubject implements Subject
????{
????@Override
????public void rent()
????{
????System.out.println("I want to rent my house");
????}
????@Override
????public void hello(String str)
????{
????System.out.println("hello: " + str);
????}
????}
???????????????????????????????????????????????????????????????? InvocationHandler ?????????????????????????????????
public class DynamicProxy implements InvocationHandler
{
//????????????????????????
private Object subject;
// ??????????????????????????????
public DynamicProxy(Object subject)
{
this.subject = subject;
}
@Override
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;
}
}
??????
???·???
??????????????????
2023/3/23 14:23:39???д?ò??????????
2023/3/22 16:17:39????????????????????Щ??
2022/6/14 16:14:27??????????????????????????
2021/10/18 15:37:44???????????????
2021/9/17 15:19:29???·???????·
2021/9/14 15:42:25?????????????
2021/5/28 17:25:47??????APP??????????
2021/5/8 17:01:11