2011-01-24 30 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