解析文件
AutoJsonRpcServiceImplExporter
JsonServiceExporter
AutoJsonRpcServiceImplExporter
路径:com.googlecode.jsonrpc4j.spring.AutoJsonRpcServiceImplExporter
AutoJsonRpcServiceImplExporter 是与spring 自动注册service启动类
实现 BeanFactoryPostProcessor 后置拦截器,具体了解可点击访问
postProcessBeanFactory() 是重载实现接口方法 也是该类入口;
public void postProcessBeanFactory(ConfigurableListableBeanFactory beanFactory) throws BeansException {
//扫描注解@JsonRpcService 的类
DefaultListableBeanFactory defaultListableBeanFactory = (DefaultListableBeanFactory) beanFactory;
Map servicePathToBeanName = findServiceBeanDefinitions(defaultListableBeanFactory);
//循环注册Service,
for (Entry entry : servicePathToBeanName.entrySet()) {
registerServiceProxy(defaultListableBeanFactory, makeUrlPath(entry.getKey()), entry.getValue());
}
}
findServiceBeanDefinitions() 扫描注解@JsonRpcService 的类 形成 key-value 结果集 key 是jsonrpc注解值,value 是 servicebeanName名称. serviceBeanNames.put(path, beanName);
//TODO
registerServiceProxy(defaultListableBeanFactory, makeUrlPath(entry.getKey()), entry.getValue())
entry.getKey() 是jsonrpc注解值 entry.getValue() 是 servicebeanName名称
BeanDefinitionBuilder builder = BeanDefinitionBuilder.rootBeanDefinition(JsonServiceExporter.class).addPropertyReference("service", serviceBeanName);
registerServiceProxy 方法内部做了一些属性注入
if (objectMapper != null) {
builder.addPropertyValue("objectMapper", objectMapper);
}
if (errorResolver != null) {
builder.addPropertyValue("errorResolver", errorResolver);
}
if (invocationListener != null) {
builder.addPropertyValue("invocationListener", invocationListener);
}
if (registerTraceInterceptor != null) {
builder.addPropertyValue("registerTraceInterceptor", registerTraceInterceptor);
}
if (httpStatusCodeProvider != null) {
builder.addPropertyValue("httpStatusCodeProvider", httpStatusCodeProvider);
}
if (convertedParameterTransformer != null) {
builder.addPropertyValue("convertedParameterTransformer", convertedParameterTransformer);
}
if (contentType != null) {
//设置response类型
builder.addPropertyValue("contentType", contentType);
}
builder.addPropertyValue("backwardsCompatible", backwardsCompatible);
builder.addPropertyValue("rethrowExceptions", rethrowExceptions);
builder.addPropertyValue("allowExtraParams", allowExtraParams);
builder.addPropertyValue("allowLessParams", allowLessParams);
builder.addPropertyValue("shouldLogInvocationErrors", shouldLogInvocationErrors);
defaultListableBeanFactory 是IOC始祖,提定义Bean注册监听;
defaultListableBeanFactory.registerBeanDefinition(servicePath, builder.getBeanDefinition());
JsonServiceExporter 实现HtttpHandler,最终容器注册是这个Bean的实例.
class JsonServiceExporter extends AbstractJsonServiceExporter implements HttpRequestHandler {
//jsonRpcServer 顶级实例
private JsonRpcServer jsonRpcServer;
/**
* {@inheritDoc}
*/
@Override
protected void exportService() {
jsonRpcServer = getJsonRpcServer();
}
/**
* {@inheritDoc}
*/
public void handleRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
//核心方法执行入口
jsonRpcServer.handle(request, response);
response.getOutputStream().flush();
}
}
JsonRpcMultiServer
/**
* A multiple service dispatcher that supports JSON-RPC "method" names
* that use dot-notation to select a server endpoint. For example:
*
* {
* "jsonrpc": "2.0",
* "method": "service.method",
* "params": {"foo", "bar"},
* "id": 1
* }
*
* An example of using this class is:
*
* JsonRpcMultiServer rpcServer = new JsonRpcMultiServer();
* rpcServer.addService("Foo", new FooService())
* .addService("Bar", new BarService());
*
* A client can then call a test(String, String) method on the Foo service
* like this:
*
* {
* "jsonrpc": "2.0",
* "method": "Foo.test",
* "params": ["some", "thing"],
* "id": 1
* }
*
*/
转载至链接:https://my.oschina.net/guoenzhou/blog/1833075