Injectable FacesContext

When you write a producer for FacesContext like this

@Produces
public FacesContext produce() {
  return FacesContext.getCurrentInstance();
}

do not forget to specify a scope! Otherwise you will get an Exception when accessing the object when it is injected in a session scoped bean. So the correct version looks like this:

@Produces
@RequestScoped
public FacesContext produce() {
  return FacesContext.getCurrentInstance();
}

Leave a Comment