1 | DemoService demoService = (DemoService) context.getBean("demoService"); |
由上一节分析知道调用demoService实际上是返回的proxy0(不一定是0)代理类,由于proxy0源码没有,所以debug会直接进InvokerInvocationHandler
InvokerInvocationHandler.invoke:1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29public class InvokerInvocationHandler implements InvocationHandler {
private final Invoker<?> invoker;
public InvokerInvocationHandler(Invoker<?> handler) {
this.invoker = handler;
}
public Object invoke(Object proxy, Method method, Object[] args) throws Throwable {
//proxy=proxy0 代理类本身 method:调用方法 args:方法参数
String methodName = method.getName();
Class<?>[] parameterTypes = method.getParameterTypes();
if (method.getDeclaringClass() == Object.class) {
return method.invoke(invoker, args);
}
if ("toString".equals(methodName) && parameterTypes.length == 0) {
return invoker.toString();
}
if ("hashCode".equals(methodName) && parameterTypes.length == 0) {
return invoker.hashCode();
}
if ("equals".equals(methodName) && parameterTypes.length == 1) {
return invoker.equals(args[0]);
}
//invoker=MockClusterInvoker
return invoker.invoke(new RpcInvocation(method, args)).recreate();
}
}