简体   繁体   English

列出由SpringMVC创建的请求范围和会话范围的bean

[英]List the request- and session-scoped beans that are created by SpringMVC

If there's a list of these already available on the web, please link it. 如果网上已经有这些列表,请链接它。 I couldn't find any such thing using the Googles. 我用Google找不到任何这样的东西。

Request-scoped beans: 请求范围的bean:

  • javax.servlet.http.HttpServletRequest

Session-scoped beans: 会话范围的bean:

  • javax.servlet.http.HttpSession

Autowiring happens once, after the object creation, and this is a main thing to keep in mind when reasoning about autowiring and different scopes. 自动装配在对象创建之后发生一次,这是在考虑自动装配和不同作用域时要牢记的主要内容。

About your question, there's in fact no issue when it comes to injecting longer living beans inside a short-lived beans. 关于您的问题,实际上,将更长寿命的豆子注入短命的豆子中没有问题。 Its only important that you're aware of it and that it fits your semantic. 重要的是您要意识到它并且适合您的语义。

Other way around is a bit trickier. 其他方法比较棘手。 So injecting shorter lived beans, inside a longer lived beans. 因此,在寿命更长的豆子中注入寿命较短的豆子。 The proper way for doing this is leaning on proxies. 这样做的正确方法是依靠代理。 If you're injecting a request-scoped bean inside a session-scoped bean, and if the request-scoped bean is proxied, than the proxy will be created only once, but will create a request bean on each request. 如果要在会话范围的Bean内注入请求范围的Bean,并且代理了请求范围的Bean,则代理将仅创建一次,但将在每个请求上创建一个请求Bean。

Its a simplification of what is described in the docs and available at http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-scopes-other-injection 它简化了文档中描述的内容,并且可以在http://docs.spring.io/spring/docs/current/spring-framework-reference/html/beans.html#beans-factory-scopes-other-injection获得

Well it really depends on what you've told Spring to create, but you'd see this in a default setup. 好吧,这实际上取决于您告诉Spring创建的内容,但是您会在默认设置中看到它。

REQUEST: 请求:

    for(String key : Collections.list(request.getAttributeNames())) {
        System.out.println( key );
    }

RESULT: 结果:

org.springframework.web.context.request.async.WebAsyncManager.WEB_ASYNC_MANAGER
org.springframework.web.servlet.DispatcherServlet.CONTEXT
org.springframework.web.servlet.DispatcherServlet.LOCALE_RESOLVER
org.springframework.web.servlet.HandlerMapping.bestMatchingPattern
org.springframework.web.servlet.DispatcherServlet.OUTPUT_FLASH_MAP
org.springframework.web.servlet.DispatcherServlet.FLASH_MAP_MANAGER
org.springframework.core.convert.ConversionService
org.springframework.web.servlet.DispatcherServlet.THEME_SOURCE
org.springframework.web.servlet.HandlerMapping.pathWithinHandlerMapping
org.springframework.web.servlet.HandlerMapping.uriTemplateVariables
org.springframework.web.servlet.DispatcherServlet.THEME_RESOLVER

SESSION: 会议:

    for(String key : Collections.list(session.getAttributeNames())) {
        System.out.println( key );
    }

RESULT: 结果:

(empty)

You can autowire anything that Spring can build. 您可以自动装配Spring可以构建的任何东西。 Whether or not you should is another matter. 你是否应该是另一回事。

For example, it is utterly senseless to autowire a bean with Step scope (from batch processing) to a request scoped bean. 例如,将具有Step范围(从批处理)的bean自动连接到请求范围的bean完全是毫无意义的。

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

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