为什么要在JSF中使SessionScoped ManagedBean线程安全? [英] Why should we make a SessionScoped ManagedBean thread safe in JSF?

查看:146
本文介绍了为什么要在JSF中使SessionScoped ManagedBean线程安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Application-Scope可在多个用户之间持久存在,因此很明显,我们应该确保所有ApplicationScoped ManagedBeans都是线程安全的.

I know that Application-Scope persists across multiple users, so it's obvious that we should make sure that all the ApplicationScoped ManagedBeans are thread safe.

我还了解到,我们不需要关心RequestScoped ManagedBean的线程安全性.这是因为它仅在一个HTTP请求中持续,并且如果被引用,则会为每个请求重新实例化.

I also understand that we don't need to care about thread safety for a RequestScoped ManagedBean. That's because it last only for one HTTP request, and is newly instantiated for each request if it's referenced.

但是我不太确定为什么我们要担心SessionScoped ManangedBean的线程安全性.即使它在多个请求中仍然存在,每个用户都可以得到自己的实例,对吗?

But I am not quite sure why we should worry about thread safety for a SessionScoped ManangedBean. Even though it persists across multiple requests, each individual user gets his/her own instance of it, right?

那么,为什么在SessionScoped ManagedBeand的情况下我们需要担心线程安全,这是否也适用于ViewScoped ManagedBean? ViewScope在同一视图的两个连续请求中仍然存在,对吗?

So, why do we need to worry about thread safety in case of SessionScoped ManagedBeand, and does that apply to ViewScoped ManagedBean too? ViewScope persists across 2 consecutive requests for the same view, right?

推荐答案

如果您已经担心某个范围内数据的线程安全性,那么该数据很可能属于一个更窄的范围(即存在缺陷).高级设计).如果将数据放在正确的范围内,则完全没有理由担心线程安全性.我假设您的bean是正确设计的,它们不会在获取器.

If you already worry about the threadsafety of the data in a certain scope, then the data most likely belongs in a more narrow scope (i.e. there is a flaw in the high level design). If the data is been put in the right scope, then there's totally no reason to worry about threadsafety. I assume that your beans are designed the right way that they aren't doing any business logic in the getters.

将应用程序范围用于应用程序范围的数据/常量,例如下拉列表,每个人都相同.将会话范围用于客户端特定的数据,例如登录的用户和用户首选项(语言等).将视图范围用于启用了丰富的ajax的动态视图(基于ajax的验证,渲染等).将请求范围用于简单和非ajax表单/表示.

Use the application scope for application wide data/constants, such as dropdown lists which are the same for everyone. Use the session scope for client specific data, such as the logged-in user and user preferences (language, etc). Use the view scope for rich ajax-enabled dynamic views (ajaxbased validation, rendering, etc). Use the request scope for simple and non-ajax forms/presentations.

这篇关于为什么要在JSF中使SessionScoped ManagedBean线程安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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