File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes JSF and the fly likes How do you preload a JSF application scope bean? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » JSF
Bookmark "How do you preload a JSF application scope bean?" Watch "How do you preload a JSF application scope bean?" New topic

How do you preload a JSF application scope bean?

Ed Pepling

Joined: Apr 06, 2009
Posts: 1
I've looked hard and could not find any answers.

I tried to preload in the web app startup a JSF application scope bean. What's the best way of doing it?

I hope I can write a servlet which calls the JSF components, but it has to be inside the JSF context. How to do it?

I also thought about setting up a listener, but that listerner must be able in some way intialise the JSF context and its appliation scope bean. so I don't know how to do it.

Any suggestions would be great. with some code example would be much appreciated.


Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Application scoped beans are placed in the application scope of the underlying context.
Assuming that your JSF app runs on top of Servlet context, then the application scoped beans are just set as attributes of ServletContext.

In a Listener you just have access to the ServletContext. Place the application scoped bean in there with the managed bean name as attribute name.
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

The JSF context is not some sort of ongoing environment or control program. It's created by the JSF Servlet when a JSF-mapped URL is received, used to invoke the JSF resources, then discarded once the response has been sent back to the client. So there's nothing there for you to worry about.

JSF beans are no different than non-JSF beans, except that the JSF servlet may have created and initialized them using the specifications in faces-config rather than having the job done by explicit application logic. All this is very good, since it means that you can use JSF to do what JSF does best, while still being able to use raw JSPs, servlets, and even other frameworks such as Struts in the same webapp.

You're on the right track. If you create what I call a "null servlet" - a servlet with an init() method, but no get/post handlers, you can construct the application-scope bean in the init() method and store it in the application context. JSF will then be able to use - just define a managed bean with the name name that you stored it under in your servlet. Actually, lack of get/post logic is optional, but it's not used for the specific purpose of constructing the app-scope bean. Also make sure you configure web.xml to auto-start the servlet.

An IDE is no substitute for an Intelligent Developer.
Carlo Lopez

Joined: Mar 05, 2011
Posts: 17

A simple solution that I have found in the book "JavaServer Faces 2.0: The Complete Reference" (Burns & Schalks, 2010), in the page 272, is to use the "eager" attribute on the ManagedBean annotation, for example:

in the page 273 it says "This attribute advises the JSF runtime that this bean instance ... must be
instantiated at startup time, before the container has served any requests"

If you want to reach the stars, then divide and conquer
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 17410

Technically, you just woke a zombie, carlo, but considering the context, we'll forgive you

I ran off and checked the JavaDocs. What you said was true, but there is a little "asterisk clause" that does with it:

Oracle JavaDocs wrote:
If the value of the eager() attribute is true, and the managed-bean-scope value is "application", the runtime must instantiate this class when the application starts. This instantiation and storing of the instance must happen before any requests are serviced. If eager is unspecified or false, or the managed-bean-scope is something other than "application", the default "lazy" instantiation and scoped storage of the managed bean happens.

I agree. Here's the link:
subject: How do you preload a JSF application scope bean?
It's not a secret anymore!