Dejell
Dejell

Reputation: 14317

FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()) returns null

I am using Spring, Hibernate and JSF.

In order to get a bean from application context I write:

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

However, the FacesContextUtils.getWebApplicationContext(FacesContext.getCurrentInstance()) returns null, so calling getBean() on it throws NullPointerException

Do I need to define anything anywhere?

EDITED

I need to get the bean from application context and not jsf bean

Upvotes: 3

Views: 6455

Answers (1)

Bozho
Bozho

Reputation: 597124

FacesContext.getCurrentInstance() returns null if your are not within a request that is handled by the FacesServlet. Make sure you are handling a request passed through that servlet.

Update: If the faces context is not null, the reason why the application context may be null is that there is no servlet context attribute named org.springframework.web.context.WebApplicationContext.ROOT. This means spring is not initialized properly. Make sure you have either the spring ContextLoaderListener mapped in web.xml, or the DispatcherServlet

Upvotes: 6

Related Questions