什么是无作用域 bean 以及何时使用它? [英] what is none scope bean and when to use it?

查看:26
本文介绍了什么是无作用域 bean 以及何时使用它?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有人能解释一下什么是无作用域以及它的目的吗?

could some explain what a none scope is and purpose of it?

假设我有一个 bean

Suppose if i have a bean in

request scope as r1

session scope as s1

application scope a1

并说我将无作用域 bean n1 注入到上述每个作用域中,然后我发现 n1 得到当它的父 bean[r1/s1/a1] 被实例化时,为每个父 bean 实例化.

and say i inject none scope bean n1 in to each of above scopes then i find that n1 gets instantiated for each parent bean when ever its parent bean[r1/s1/a1] is instantiated.

a1 中的无作用域 bean 在 a1 中始终可用,因为 a1 是应用程序作用域.s1 中的 none 作用域 bean 仅在 s1 未被销毁且 s1 被创建时才可用n1 再次被实例化并提供给它.

none scope bean in a1 is available throughout in a1 since a1 is appl scope. none scope bean in s1 is available only until s1 is not destroyed and when s1 is created again n1 is instanciated and made available to it.

正确吗?

使用它的目的是什么?只是为了避免我们自己创建这样的 bean?

and what the purpose of using it? only to avoid creating such bean our self?

非常感谢

推荐答案

none 的 bean="http://docs.oracle.com/javaee/6/api/javax/faces/bean/NoneScoped.html" rel="noreferrer">@NoneScoped 注释将是在每个引用 bean 的 EL 表达式上创建.它没有被 JSF 存储在任何地方.如有必要,调用者必须自己存储评估的引用.

A bean with a <managed-bean-scope> of none or a @NoneScoped annotation will be created on every single EL expression referencing the bean. It isn't been stored by JSF anywhere. The caller has got to store the evaluated reference itself, if necessary.

例如视图中的以下内容

<p>#{noneScopedBean.someProperty}</p>
<p>#{noneScopedBean.someProperty}</p>
<p>#{noneScopedBean.someProperty}</p>

在非作用域 bean 上将在请求期间 构造 bean 3(三)次.对 bean 的每次访问都会提供一个完全独立的 bean,该 bean 在属性访问后立即被丢弃.

on a none-scoped bean will construct the bean 3 (three) times during a request. Every access to the bean gives a completely separate bean which is been garbaged immediately after the property access.

但是,例如会话范围bean中的以下内容

However, the following in for example a session scoped bean

@ManagedProperty("#{noneScopedBean}")
private NoneScopedBean noneScopedBean;

将使它与会话范围的 bean 实例一样长.您应该只确保在视图中通过 #{sessionScopedBean.noneScopedBean.someProperty} 访问它.

will make it to live as long as the session scoped bean instance. You should only make sure that you access it in the view by #{sessionScopedBean.noneScopedBean.someProperty} instead.

因此,当您希望无作用域数据可用作任意 bean 中的托管属性时,它可能很有用.

So it may be useful when you want scope-less data being available as a managed property in an arbitrary bean.

这篇关于什么是无作用域 bean 以及何时使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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