RpcServiceProxy#doCreateRequestCallback()看起来像要覆盖的方法.很简单.我只是看不出如何让GWT使用我的新课程.
陈述问题的另一种方式是
为了包装每个AsynCallback< T>传递给任何需要覆盖RemoteServiceProxy#doCreateRequestCallback()的RemoteService,因为每个AsynCallback< T>在RPC调用发生之前将其传递给此处.How do I make GWT use my own subclass of
RpcServiceProxy
?
以下是执行此操作的步骤:
正如@ChrisLercher所提到的,您需要定义自己的代理生成器,以便在每次生成RemoteService代理时进入.首先扩展ServiceInterfaceProxyGenerator并重写#createProxyCreator().
/** * This Generator extends the default GWT {@link ServiceInterfaceProxyGenerator} and replaces it in the * co.company.MyModule GWT module for all types that are assignable to * {@link com.google.gwt.user.client.rpc.RemoteService}. Instead of the default GWT {@link ProxyCreator} it provides the * {@link MyProxyCreator}. */ public class MyServiceInterfaceProxyGenerator extends ServiceInterfaceProxyGenerator { @Override protected ProxyCreator createProxyCreator(JClassType remoteService) { return new MyProxyCreator(remoteService); } }
在MyModule.gwt.xml中,使用延迟绑定来指示GWT在生成RemoteService类型时使用您的代理生成器进行编译:
<generate-with class="com.company.ourapp.rebind.rpc.MyServiceInterfaceProxyGenerator"> <when-type-assignable class="com.google.gwt.user.client.rpc.RemoteService"/> </generate-with>
扩展ProxyCreator并覆盖#getProxySupertype().在MyServiceInterfaceProxyGenerator#createProxyCreator()中使用它,以便您可以为所有生成的RemoteServiceProxies定义基类.
/** * This proxy creator extends the default GWT {@link ProxyCreator} and replaces {@link RemoteServiceProxy} as base class * of proxies with {@link MyRemoteServiceProxy}. */ public class MyProxyCreator extends ProxyCreator { public MyProxyCreator(JClassType serviceIntf) { super(serviceIntf); } @Override protected Class<? extends RemoteServiceProxy> getProxySupertype() { return MyRemoteServiceProxy.class; } }
确保MyProxyCreator和MyServiceInterfaceProxyGenerator都位于一个不会被GWT交叉编译为javascript的包中.否则你会看到如下错误:
[ERROR] Line XX: No source code is available for type com.google.gwt.user.rebind.rpc.ProxyCreator; did you forget to inherit a required module?
您现在可以扩展RemoteServiceProxy并覆盖#doCreateRequestCallback()!在这里,您可以执行任何您喜欢的操作,并将其应用于发送到服务器的每个回调.确保将此类以及此处使用的任何其他类(在我的案例中为AsyncCallbackProxy)添加到要进行交叉编译的客户端程序包中.
/** * The remote service proxy extends default GWT {@link RemoteServiceProxy} and proxies the {@link AsyncCallback} with * the {@link AsyncCallbackProxy}. */ public class MyRemoteServiceProxy extends RemoteServiceProxy { public MyRemoteServiceProxy(String moduleBaseURL, String remoteServiceRelativePath, String serializationPolicyName, Serializer serializer) { super(moduleBaseURL, remoteServiceRelativePath, serializationPolicyName, serializer); } @Override protected <T> RequestCallback doCreateRequestCallback(RequestCallbackAdapter.ResponseReader responseReader, String methodName, RpcStatsContext statsContext, AsyncCallback<T> callback) { return super.doCreateRequestCallback(responseReader, methodName, statsContext, new AsyncCallbackProxy<T>(callback)); } }
参考文献:
> DevGuideCodingBasicsDeferred.html
> An example applied to performance tracking