2011-01-24 31 views
2

我正在使用Spring,Hibernate和JSF。FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance())返回null

为了得到从应用程序上下文的bean我写:

public static Object findBean(String name) { 
    return FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()).getBean(name); 
} 

然而,FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance())返回NULL,所以调用getBean()它抛出NullPointerException

我需要的任何地方定义什么?

EDITED

我需要的,如果你的是不是由FacesServlet处理的要求内得到应用上下文的bean,而不是JSF豆

+3

什么是确切的引用'null`?那些是静态方法,它们不可能抛出NPE。你不是说`FacesContext#getCurrentInstance()`返回null,而且NPE实际上是在`getWebApplicationContext()`方法中抛出的吗? – BalusC 2011-01-24 19:32:25

+0

FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance())返回null。 FacesContext.getCurrentInstance()不为null! – Dejell 2011-01-24 19:38:05

回答

6

FacesContext.getCurrentInstance()回报null。确保你正在处理通过该servlet传递的请求。

更新:如果faces上下文不为null,则应用程序上下文可能为null的原因是没有名为org.springframework.web.context.WebApplicationContext.ROOT的servlet上下文属性。这意味着弹簧没有正确初始化。确保你的弹簧ContextLoaderListener映射到web.xmlDispatcherServlet