This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSF and the fly likes Application Scoped Managed Bean Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSF
Bookmark "Application Scoped Managed Bean" Watch "Application Scoped Managed Bean" New topic

Application Scoped Managed Bean

Abiodun Adisa
Ranch Hand

Joined: Jan 17, 2002
Posts: 495
I intend to load some data from the database and loading it into the application Scope Context once during the lifetime of my application so it can be used by every managed Bean. I considered putting this code inside a managed bean marked as Application Scoped. But i realise that the Application Scoped Managed Bean does not execute unless an JSF action references a method contained in it, so in essence if nothing references that Application Scoped Bean. it might never get called...Suggestions are welcome to resolve this
[ October 23, 2007: Message edited by: Abiodun Adisa ]
Tim Holloway
Saloon Keeper

Joined: Jun 25, 2001
Posts: 15960

1. Create a basic servlet set up to initialize on startup.

2. Add an init() method to the servlet that constructs the bean and stores it at application scope.

3. Use the normal JSF processes to reference this bean as needed.

There's no difference between beans constructed and stored by JSF code and their counterparts constructed and stored by non-JSF code. All JSF is doing here is handling the construction for you (if you ask it to).

Customer surveys are for companies who didn't pay proper attention to begin with.
A. Dusi
Ranch Hand

Joined: Sep 27, 2004
Posts: 114
The best way to achieve this is to use a ServetContextListener instead of managed bean creation facility. Use its contextInitialized method to put the variables in application scope.
Lee Mark
Ranch Hand

Joined: Mar 05, 2009
Posts: 46
can you please elaborate this ?
i mean i can track the context initilized by ServletContextListner but how can i initilize a manage bean with fully blown dependencies, and how can i put this to application scope
can you please elaborate with sample ..

thanks in advance.
Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1280

Hi Lee,

you can register a class which implements ServletContextListener (for Servlet API 2.5 in your web.xml descriptor) which is called when a web application is started or the application context is reloaded for example with the Tomcat manager application.

You can store the application scoped bean or something as a Servlet context attribute. The listener will automatically receive a ServletContextEvent which in turn gives you access to context attributes. Where you get your application specific dependencies from for the object to create is of course up to you.

Just have a look to the Servlet API documentation for the said listener. If you still have problems to find out how it works feel free to ask

Lee Mark
Ranch Hand

Joined: Mar 05, 2009
Posts: 46
Thanks Macro,


i am getting 'var' as null at deployment time,
how to inject properties that i dont understand,
can you please tell me,
Thanks again
Marco Ehrentreich
best scout

Joined: Mar 07, 2007
Posts: 1280

No no, this was not exactly what I described

You only USE the context listener to create an application scoped object but the listener class itself is not the application scoped object! You create the said object inside the listener and store it as a Servlet context attribute. Unfortunately I don't even know how exactly you can access scoped objects inside JSF 1.x (I guess it's 1.x) but I'm sure there's an easy way to do so.

Have a look at the Servlet API. The ServletContextEvent gives you access to the ServletContext which in turn has a method "setAttribute()" which can be used to store context (=application) wide objects which can then be accessed from insided JSP or JSF pages.

With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
subject: Application Scoped Managed Bean
Similar Threads
Obtaining an instance of a Managed Bean declared with Application Level Scope
Injecting a session bean into an application bean
ViewScoped bean using ManagedProperty
Loading DB data when web app context initialized
Specific method for bean JSF!