Struts2 中出现的Jdk动态署理实例和方法
Struts2 中出现的Jdk动态代理实例和方法
package com.opensymphony.xwork2.inject; import java.lang.reflect.InvocationHandler; import java.lang.reflect.Method; import java.lang.reflect.InvocationTargetException; import java.lang.reflect.Proxy; import java.util.List; import java.util.ArrayList; /** * Context of a dependency construction. Used to manage circular references. * * @author crazybob@google.com (Bob Lee) */ class ConstructionContext<T> { T currentReference; boolean constructing; List<DelegatingInvocationHandler<T>> invocationHandlers; T getCurrentReference() { return currentReference; } void removeCurrentReference() { this.currentReference = null; } void setCurrentReference(T currentReference) { this.currentReference = currentReference; } boolean isConstructing() { return constructing; } void startConstruction() { this.constructing = true; } void finishConstruction() { this.constructing = false; invocationHandlers = null; } //此处应该是该方法的核心 // Object createProxy(Class<? super T> expectedType) { // TODO: if I create a proxy which implements all the interfaces of // the implementation type, I'll be able to get away with one proxy // instance (as opposed to one per caller). if (!expectedType.isInterface()) { throw new DependencyException( expectedType.getName() + " is not an interface."); } if (invocationHandlers == null) { invocationHandlers = new ArrayList<DelegatingInvocationHandler<T>>(); } DelegatingInvocationHandler<T> invocationHandler = new DelegatingInvocationHandler<T>(); invocationHandlers.add(invocationHandler); return Proxy.newProxyInstance( expectedType.getClassLoader(), // 加载接口的类加载器 new Class[] { expectedType }, // 接口集合 invocationHandler // handler处理类 ); } // 注意此处调用方法 中的参数 和值 认真看看希望能看出端倪来。此处很妙 void setProxyDelegates(T delegate) { if (invocationHandlers != null) { for (DelegatingInvocationHandler<T> invocationHandler : invocationHandlers) { invocationHandler.setDelegate(delegate); } } } // 内部静态类 static class DelegatingInvocationHandler<T> implements InvocationHandler { T delegate; public Object invoke(Object proxy, Method method, Object[] args) throws Throwable { if (delegate == null) { throw new IllegalStateException( "Not finished constructing. Please don't call methods on this" + " object until the caller's construction is complete."); } try { // method.invoke 中的参数 代理类 和要传入的参数 return method.invoke(delegate, args); //method方法应该是delegate接口中的 } catch (IllegalAccessException e) { throw new RuntimeException(e); } catch (IllegalArgumentException e) { throw new RuntimeException(e); } catch (InvocationTargetException e) { throw e.getTargetException(); } } // 此处为何留一个接口方法呢? void setDelegate(T delegate) { this.delegate = delegate; } } }