托管 Bean 未在 @ApplicationScoped 和 eager=true 处实例化
Managed Bean not being instantiated at @ApplicationScoped and eager=true
我有以下 UserView
class:
@ManagedBean(name="usersView", eager=true)
@ApplicationScoped
public class UserView
{
...
public void setUsers(...)
{
...
}
}
然后我有另一个 class,UserService
尝试访问这个 bean 然后调用这个方法,如下所示:
UserView usersView = (UserView) FacesContext.getCurrentInstance().getExternalContext().getSessionMap("usersView");
usersView.setUsers(...)
我的问题是,usersView
一直返回为 null,因此无法调用该方法。
我的最终目标是在 PrimeFaces
datatable
中使用此数据,如下所示:
<p:dataTable var="user" value="#{usersView.users}" ...>
我也试过将范围更改为 SessionScoped
,但它仍然为空,我不知道为什么。
如有任何建议,我们将不胜感激。
如果 bean 尚不存在,getExternalContext().getXxxMap().get("beanName")
方法将不会自动创建它们。托管 bean 只会在评估引用 bean 的 EL 表达式时自动创建。访问范围图不会那样做。为此,请在源 bean 中使用 @ManagedProperty
。您可以在其值中指定一个 EL 表达式。
@ManagedProperty("#{userView}")
private UserView userView; // +setter (no getter required).
请注意,这仅在源 bean 具有与目标 bean 相同或更窄的范围时才有效。如果不是这种情况,请考虑使用 CDI 而不是 JSF 来管理 bean。然后,您可以使用 @Inject
而不是 @ManagedProperty
(顺便说一下,它不需要 setter)。此外,@ManagedBean
和朋友们在即将推出的 JSF 2.3 中是 deprecated。
至于eager=true
,这只对@ApplicationScoped
个bean有影响。另见 javadoc(强调我的)。
...
If the value of the eager() attribute is true, and the managed-bean-scope value is "application", the runtime must instantiate this class when the application starts. This instantiation and storing of the instance must happen before any requests are serviced. If eager is unspecified or false, or the managed-bean-scope is something other than "application", the default "lazy" instantiation and scoped storage of the managed bean happens.
...
另请参阅:
- How to choose the right bean scope?
- Get JSF managed bean by name in any Servlet related class
- Backing beans (@ManagedBean) or CDI Beans (@Named)?
我有以下 UserView
class:
@ManagedBean(name="usersView", eager=true)
@ApplicationScoped
public class UserView
{
...
public void setUsers(...)
{
...
}
}
然后我有另一个 class,UserService
尝试访问这个 bean 然后调用这个方法,如下所示:
UserView usersView = (UserView) FacesContext.getCurrentInstance().getExternalContext().getSessionMap("usersView");
usersView.setUsers(...)
我的问题是,usersView
一直返回为 null,因此无法调用该方法。
我的最终目标是在 PrimeFaces
datatable
中使用此数据,如下所示:
<p:dataTable var="user" value="#{usersView.users}" ...>
我也试过将范围更改为 SessionScoped
,但它仍然为空,我不知道为什么。
如有任何建议,我们将不胜感激。
如果 bean 尚不存在,getExternalContext().getXxxMap().get("beanName")
方法将不会自动创建它们。托管 bean 只会在评估引用 bean 的 EL 表达式时自动创建。访问范围图不会那样做。为此,请在源 bean 中使用 @ManagedProperty
。您可以在其值中指定一个 EL 表达式。
@ManagedProperty("#{userView}")
private UserView userView; // +setter (no getter required).
请注意,这仅在源 bean 具有与目标 bean 相同或更窄的范围时才有效。如果不是这种情况,请考虑使用 CDI 而不是 JSF 来管理 bean。然后,您可以使用 @Inject
而不是 @ManagedProperty
(顺便说一下,它不需要 setter)。此外,@ManagedBean
和朋友们在即将推出的 JSF 2.3 中是 deprecated。
至于eager=true
,这只对@ApplicationScoped
个bean有影响。另见 javadoc(强调我的)。
...
If the value of the eager() attribute is true, and the managed-bean-scope value is "application", the runtime must instantiate this class when the application starts. This instantiation and storing of the instance must happen before any requests are serviced. If eager is unspecified or false, or the managed-bean-scope is something other than "application", the default "lazy" instantiation and scoped storage of the managed bean happens.
...
另请参阅:
- How to choose the right bean scope?
- Get JSF managed bean by name in any Servlet related class
- Backing beans (@ManagedBean) or CDI Beans (@Named)?