JSF - How do I force an application-scoped bean to instantiate at application startup?

It doesn't need to be that complicated. Just instantiate the bean and put it in the application scope with the same managed bean name as key. JSF will just reuse the bean when already present in the scope.

With JSF on top of Servlet API, the ServletContext represents the application scope (as HttpSession represents the session scope and HttpServletRequest represents the request scope, each with setAttribute() and getAttribute() methods). In a nut.

My first thought was to use an old style ServletContextListener contextInitialized() method and from there use an ELResolver to manually request the instance of my managed bean (thus forcing the initialization to happen). Unfortunately, I can't use an ELResolver to trigger the initialization at this stage because the ELResolver needs a FacesContext and the FacesContext only exists during the lifespan of a request. It doesn't need to be that complicated.

Just instantiate the bean and put it in the application scope with the same managed bean name as key. JSF will just reuse the bean when already present in the scope. With JSF on top of Servlet API, the ServletContext represents the application scope (as HttpSession represents the session scope and HttpServletRequest represents the request scope, each with setAttribute() and getAttribute() methods).

In a nut: public void contextInitialized(ServletContextEvent event) { event. GetServletContext(). SetAttribute("bean", new Bean()); } where "bean" should be the same as the of the application scoped bean in faces-config.xml.

Just for the record, on JSF 2. X all you need to do is to add eager=true to @ManagedBean on an @ApplicationScoped bean. @ManagedBean(eager=true) @ApplicationScoped public class Bean { // ... } It will then be auto-instantiated at application startup.

1 1+ Simply nice explanation. Learning from you. – Jigar Joshi Aug 30 '10 at 13:25 +1 for an effective solution.

One little question: is it officially ok to do that as per the spec, or does it rely on some JSF implementation details? I mean, a JSF implementation could decide to keep track of whether an application bean was instantiated in a completely non-obvious way and would then recreate the bean, for instance. – ewernli Aug 30 '10 at 13:40 @BalusC That was so simple and it works.

I had avoided using the setAttribute() method in the ServletContext because I thought it would interfere with JSF, but apparently not. PS: Love your page at blogspot. Com - your old article on using DataTables was helpful.

– Jim Tough Aug 30 '10 at 13:45 @Jim: you're welcome. @ewernli: the spec doesn't explicitly allow that, but it does also not expliticly disallow that. The spec however describes that a managed bean must be created when not present in the scope.

– BalusC Aug 30 '10 at 14:02 @BalusC, what if application scoped bean has properties that is either JSF managed bean or Spring bean, how to initialize it with fully flagged dependencies? – Jigar Joshi Aug 30 '107 at 6:55.

As far as I know, you can't force a managed bean to be instantiated at application startup. Maybe you could use a ServletContextListener which, instead of instantiating your managed bean, will perform all the database operations itself? Another solution might be to instantiate your bean manually at application startup, and then set the bean as an attribute of your ServletContext.

Here is a code sample: public class MyServletListener extends ServletContextListener { public void contextInitialized(ServletContextEvent sce) { ServletContext ctx = sce. GetServletContext(); MyManagedBean myBean = new MyManagedBean(); ctx. SetAttribute("myManagedBean", myManagedBean); } } In my opinion, this is far from clean code, but it seems like it does the trick.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions