我正在使用 Spring、Hibernate 和 JSF。
为了从应用程序上下文中获取 bean,我编写:
public static Object findBean(String name) {
return FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()).getBean(name);
}
但是,那FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance())
返回 null,因此调用getBean()
其上抛出NullPointerException
我需要在任何地方定义任何东西吗?
EDITED
我需要从应用程序上下文中获取 bean,而不是 jsf bean
FacesContext.getCurrentInstance()
回报null
如果您不在由处理的请求之内FacesServlet
。确保您正在处理通过该 servlet 传递的请求。
Update:如果faces上下文不为空,则应用程序上下文可能为空的原因是没有名为的servlet上下文属性org.springframework.web.context.WebApplicationContext.ROOT
。这意味着 spring 没有正确初始化。确保您有弹簧ContextLoaderListener
映射到web.xml
, 或者DispatcherServlet
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)