【发布时间】:2011-06-14 17:52:21
【问题描述】:
我正在使用 Spring、Hibernate 和 JSF。
为了从应用程序上下文中获取一个 bean,我编写了:
public static Object findBean(String name) {
return FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()).getBean(name);
}
但是,FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()) 返回 null,因此在其上调用 getBean() 会引发 NullPointerException
我需要在任何地方定义任何东西吗?
已编辑
我需要从应用程序上下文中获取 bean,而不是 jsf bean
【问题讨论】:
-
null到底是什么引用?这些是静态方法,它们不可能抛出 NPE。你不是说FacesContext#getCurrentInstance()返回了null并且NPE 实际上被扔进了getWebApplicationContext()方法吗? -
FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()) 返回 null。 FacesContext.getCurrentInstance() 不为空!