一个bean的Spring会话范围究竟如何? Web上下文中bean的默认范围是什么? [英] How exactly works the Spring session scope of a bean? what is the default scope of a bean in the web context?

查看:122
本文介绍了一个bean的Spring会话范围究竟如何? Web上下文中bean的默认范围是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在学习Spring MVC,我有以下疑问:

I am studying Spring MVC and I have the following doubts:


  1. 会话范围的目的究竟是什么?

  1. What exactly is the purpose of the session scope?

阅读文档我知道这可以将bean定义范围扩展到HTTP会话。仅在Web感知Spring ApplicationContext的上下文中有效。此外,每个用户会话都会创建一个新实例。

Reading the documentation I know that this scopes a bean definition to an HTTP session. Only valid in the context of a web-aware Spring ApplicationContext. And also that a new instance is created once per user session.

但究竟是什么时候使用它?为了什么目的?你能举一个实际的例子吗?

But when exactly is it used? and for what purpose? Can you make a practical example?


  1. 在Spring MVC中,Web上下文的默认范围是什么?

  1. In Spring MVC what is the default scope in the web context?

我知道在Spring中,bean的默认范围是 singleton 但是Web上下文中bean的范围?

I know that in Spring the default scope for a bean is singleton but what about the scope of a bean in the web context?

推荐答案

Ans 1)会话范围与HttpSession范围非常相似。基于会话范围范围实例化的Bean通过HTTP会话生效。与请求范围类似,它仅适用于Web感知弹簧应用程序上下文。

Ans 1) session scope is very similar to HttpSession scope. Beans instantiated based on session scope scope lives through the HTTP session. Similar to request scope, it is applicable only for web aware spring application contexts.

/** * Annotation-based configuration of session scope */ 
@Component
@Scope("session") 
public class ShopCart { }

然后

@Inject
private ShopCart cart;

答案2)默认值是单身人士无处不在。

Ans 2) Default is Singleton everywhere.

这篇关于一个bean的Spring会话范围究竟如何? Web上下文中bean的默认范围是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆