被春天范围请求困惑

时间:2015-03-06 07:13:08

标签: spring scope aop

我的场景是Spring mvc应用程序中唯一ID的跟踪信息。 首先,我实现了HandlerInterceptorAdapter

 public boolean preHandle(HttpServletRequest request, HttpServletResponse    response, Object handler) throws Exception {
     //generate UUId and put into request attribute
     request.setAttribute(USAGE_KEY, UUID.randomUUID().toString());
 }

其次,我通过自动装配的HttpServletRequest实现拦截器捕获USAGE_KEY

@Component
@Scope(value = org.springframework.web.context.WebApplicationContext.SCOPE_REQUEST, proxyMode = ScopedProxyMode.INTERFACES)
public class interceptor implement IInterceptor {
   @Autowired(required = false)
   private HttpServletRequest req;

   public synchronized String getUsageKey() {
       System.out.println("this instance ***" + this);
       System.out.println("this request hash code ***" + req.hashCode);
       System.out.println("this key in request ***" + req.getAttribute(USAGE_KEY));
       return req.getAttribute(USAGE_KEY);
   }
}

然后其他两个拦截器扩展了这个父类,让我们以interceptor1和interceptor2为例,这两个拦截器用于捕获不同的服务。但是服务将在同一个httpservletrequest中调用。

运行时,输出如下

 this instance *** interceptor1@6bb749
 this request hash code  *** 28916864 *** 
 this key in request *** deb17c31-246c-46d2-8f1e-96ec16c4a803 
 this instance *** interceptor2@17ff7c0
 this request hash code  *** 23761317*** 
 this key in request *** deb17c31-246c-46d2-8f1e-96ec16c4a803 

我对此结果感到困惑,关键是相同的,我认为不同拦截器中请求的哈希码应该相同,但它们不是。 我只发送一次请求。

0 个答案:

没有答案