Jason Irwin wrote:
I've tried a ServletContextListener, but of course that doesn't work as there is no FacesContext when the application starts up.
There's no FacesContext any other time either,
except while a JSF Request is being processed.
FacesContext is created on a per-request basis and destroyed (or at least released for garbage collection) as soon as the request response has been sent.
However, there's no difference between creating an application-scope bean manually and having JSF do it other than that the faces-config may inject initialization (post-construction) parameters that would have to be done in
Java code otherwise. Once the bean has been constructed, JSF references to the bean won't have any way of knowing whether the bean was constructed via explicit Java code or by the JSF bean manager. Specifically, if the bean is injected into another bean as part of lazy initialization of a managed bean, that should work, and likewise any page reference should work.
Kenneth's example is valid. About the only caveat is that - like all application-scope objects,
you should pay attention to possible multi-threading issues and synchronize and accesses to the bean that might corrupt the state of the bean, Which is true also in the case of JSF-only shared beans, since they're POJOs, not EJBs.