相信使用resteasy、包括dubbo的朋友对@Context这个注解一定不会陌生,我们可以通过@Context这个注解获取HttpServletRequest、HttpServletResponse等一些类的实体,那么为什么能获取到这些类的对象呢,其背后的实现原理又如何呢,闲话不多说,笔者将从resteasy源码出发,一一讲述这其中的缘由:
核心类ServletContainerDispatcher的service方法部分相关代码:
public void service(String httpMethod, HttpServletRequest request, HttpServletResponse response, boolean handleNotFound)
ResteasyProviderFactory.pushContext(HttpServletRequest.class, request);
ResteasyProviderFactory.pushContext(HttpServletResponse.class, response);
这是在调用具体rest服务之前将具体的HttpServletRequest以及HttpServletResponse实例保存到了ResteasyProviderFactory内部,然后就可以通过@Context注解获取到HttpServletRequest以及HttpServletResponse实例了,那么问题又来了,@Context注解是如何访问ResteasyProviderFactory存储的上下文变量的呢,这个就涉及到@Context注解的工作原理了,说道这个就不得不说MethodInjectorImpl,resteasy针对每个服务都会创建一个对应的MethodInjectorImpl实体,该实体有一个重要的方法:
public Object[] injectArguments(HttpRequest input, HttpResponse response)
{
try
{
Object[] args = null;
if (params != null && params.length > 0)
{
args = new Object[params.length];
int i = 0;
for (ValueInjector extractor : params)
{
args[i++] = extractor.inject(input, response);
}
}
return args;
}
对应的extractor就会从input请求中解析到对应的上下文参数,针对@Context注解创建的extrator实体类就是ContextParameterInjector,下面看下ContextParameterInjector的inject方法:
public Object inject(HttpRequest request, HttpResponse response)
{
// we always inject a proxy for interface types just in case the per-request target is a pooled object
// i.e. in the case of an SLSB
if (type.equals(Providers.class)) return factory;
if (!type.isInterface() || type.equals(SseEventSink.class))
{
return ResteasyProviderFactory.getContextData(type);
}
else if (type.equals(Sse.class))
{
return new SseImpl();
}
return createProxy();
}
之前存入到ResteasyProviderFactory的上下文参数就可以取到了!