Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

How do you preload a JSF application scope bean?

 
Ed Pepling
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi
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.

Regards

Ed
 
Bauke Scholtz
Ranch Hand
Posts: 2458
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 18164
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Carlo Lopez
Greenhorn
Posts: 17
Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"


 
Tim Holloway
Saloon Keeper
Pie
Posts: 18164
53
Android Eclipse IDE Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic