这是 Apache XMLRPC 3.x 的标准行为。http://ws.apache.org/xmlrpc/handlerCreation.html:
默认情况下,Apache XML-RPC 创建一个新对象来处理服务器端收到的每个请求。
但是,您可以模拟 XMLRPC 2.x 的行为,您可以使用 RequestProcessorFactoryFactory 注册处理程序对象而不是处理程序类。我编写了一个可以使用的自定义 RequestProcessorFactoryFactory:
public class CustomHandler implements RequestProcessorFactoryFactory {
Map<Class<?>, RequestProcessorFactory> handlers =
Collections.synchronizedMap(
new HashMap<Class<?>, RequestProcessorFactory>());
@Override
public RequestProcessorFactory getRequestProcessorFactory(Class pClass)
throws XmlRpcException {
return handlers.get(pClass);
}
public void addHandler(final Object handler) {
handlers.put(handler.getClass(), new RequestProcessorFactory() {
@Override
public Object getRequestProcessor(XmlRpcRequest pRequest)
throws XmlRpcException {
return handler;
}
});
}
}
然后可以将其与例如这样的 XMLRPC WebServer 一起使用
WebServer server = ...
PropertyHandlerMapping phm = new PropertyHandlerMapping();
server.getXmlRpcServer().setHandlerMapping(phm);
Custom sh = new CustomHandler();
phm.setRequestProcessorFactoryFactory(sh);
Object handler = ... /** The object you want to expose via XMLRPC */
sh.addHandler(handler);
phm.addHandler(serverName, handler.getClass());