什么是非范围bean以及何时使用它

什么是非范围bean以及何时使用它

本文介绍了什么是非范围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父bean [r1 / s1 / a1]被实例化。

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

a1中的所有范围bean都不在a1中,因为a1是appl范围。
s1中的无范围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 createdagain n1 is instanciated and made available to it.

这是正确的吗?

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

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

非常感谢

推荐答案

一个bean < managed-bean-scope> 或注释将在引用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.

例如。视图中的以下内容

E.g. the following in the view

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

在无范围的bean上构建 bean 3(三)请求期间的时间。对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以及何时使用它?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 11:56