简体   繁体   English

在HandlerIntercaptor中访问请求范围的bean

[英]accessing a request-scoped bean in HandlerIntercaptor

I'm very new to Spring and web development in general, so please bear with me if the question seems confused or incomplete... 我对Spring和Web开发总体而言还是个新手,所以如果这个问题看起来很困惑或不完整,请多多包涵...

I'm currently working on a Spring project in which there is a bean I'll call 'requestContext' which holds some commonly used data. 我目前正在开发一个Spring项目,该项目中有一个我称为“ requestContext”的bean,其中包含一些常用数据。 This bean is request-scoped, and it appears to get populated by a servelet filter (a child of GenericFilterBean). 该bean是请求范围的,并且似乎由Servetlet过滤器(GenericFilterBean的子级)填充。

I'm trying to access the info held by this bean from another bean (i'll call UserBean) in the preHandle method of a HandlerInterceptor. 我试图在HandlerInterceptor的preHandle方法中从另一个bean(我称为UserBean)访问此bean持有的信息。 In UserBean I use @AutoWired to access the bean as below: 在UserBean中,我使用@AutoWired如下访问bean:

@Autowired
private RequestContext requestContext;

Then in one of UserBeans methods I try to access the necessary data. 然后,在UserBeans方法之一中,我尝试访问必要的数据。 The problem is that the request context contains all null values. 问题在于请求上下文包含所有空值。 I thought there might be some lifecycle issue, being unfamiliar with filters, but using some breakpoints I can see that the filter is executed before the handlerInterceptor, and I can see the request context data being set. 我认为可能存在一些生命周期问题,不熟悉过滤器,但是使用一些断点,我可以看到过滤器是在handlerInterceptor之前执行的,并且可以看到设置了请求上下文数据。 That being the case I would expect to at least be able to access it in the preHandle method of the interceptor if not the others. 在这种情况下,我希望至少能够在其他拦截器的preHandle方法中访问它。

The rest of the application (including filter, handler interceptor) is all existing/known working code, so I don't think I have any setup issues up to the point where I'm trying to use that bean. 该应用程序的其余部分(包括过滤器,处理程序拦截器)都是现有的/已知的工作代码,因此,在尝试使用该bean之前,我认为我没有任何设置问题。 There's just some problem with either my expectations, or with how I'm trying to access it. 我的期望或我尝试访问它的方式都存在一些问题。

UPDATE: I found one example of a class that actually consumes the requestContext. 更新:我发现了一个实际使用requestContext的类的示例。 It's another filter (but implements Filter directly vs extending GenericFilterBean). 这是另一个过滤器(但是直接实现Filter而不是扩展GenericFilterBean)。 This filter calls 该过滤器调用

SpringBeanAutowiringSupport.processInjectionBasedOnCurrentContext(this)

in its init() method. 在其init()方法中。 I noticed that if I make this call just before trying to access requestContext, then it is instantiated with the values I expect (also notice that if I do it in a default constructor it does not work). 我注意到,如果我只是在尝试访问requestContext之前进行此调用,那么它将使用我期望的值实例化(还要注意,如果我在默认构造函数中执行此操作将不起作用)。 Something tells me this is not the rights solution in my case, but hopefully this sheds some light on the issue. 有人告诉我这不是我的权利解决方案,但希望这可以为您解决这个问题。

Trying to read up on SpringBeanAutowiringSupport to understand better. 尝试在SpringBeanAutowiringSupport上阅读以更好地理解。 I think if I'm understanding it correctly, this indicates my bean does not currently have access to the WebApplicationContext, so the Autowiring is not working by default until this call is made (once the call is made, subsequent requests don't seem to require it). 认为,如果我正确理解它,则表明我的bean当前无权访问WebApplicationContext,因此默认情况下,直到进行此调用后,Autowiring才默认工作(一旦进行了调用,后续请求似乎就不会要求)。 Does this indicate some issue with the way I've configured the bean (it's not registered with IoC correctly?) Aagain, please forgive my lack of knowledge on spring, I still don't know that much about this stuff like IoC... 这是否表明我配置Bean的方式存在一些问题(未在IoC中正确注册?)再次,请原谅我在春季的知识不足,我仍然对IoC之类的东西还不了解...

Well, it looks like ElderMael was onto something, and my question ends up being answered by this question: How are Spring HandlerInterceptors instantiated? 好吧,看来ElderMael遇到了什么,我的问题最终被以下问题回答: Spring HandlerInterceptors是如何实例化的?

In absence of defining a scope for the interceptor, and beans it uses, they are singletons, and they are created with one instance of the requestContext. 在没有为拦截器及其使用的bean定义范围的情况下,它们是单例的,它们是使用requestContext的一个实例创建的。 When subsequent instances are created for each request, I still have my old original instance. 为每个请求创建后续实例时,我仍然拥有旧的原始实例。 Adding the processInjectionBasedOnCurrentContext method I believe is just processing the autowiring again, and finding the newly created instance. 我相信添加processInjectionBasedOnCurrentContext方法只是在再次处理自动装配,并找到新创建的实例。 I'll have to think about the ideal way to solve that, but I think because the HandlerInterceptor has no need to retain state, I could probably make that and the related beans request-scoped as well. 我将不得不考虑解决该问题的理想方法,但是我认为因为HandlerInterceptor不需要保留状态,所以我可能可以做到这一点,并且相关的bean也可以在请求范围内进行。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM