You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Steve Torrefranca <st...@branders.com> on 2007/02/20 15:28:03 UTC
Loading application scoped managed beans on server startup
what is the best way to do this?
thank you
-Steve
Re: Loading application scoped managed beans on server startup
Posted by Craig McClanahan <cr...@apache.org>.
On 2/20/07, Steve Torrefranca <st...@branders.com> wrote:
> > Does it have to be a managed bean?
> I am afraid so, though i could be wrong. The List is used by the view
> as source for select Items. But yes I can use this a work around.
>
It's not really a "workaround".
The only thing special about managed beans are the fact that they can
get created for you, as a side effect of evaluating an expression.
But after creation the server actually puts them into one of the
normal scopes:
* Request scoped managed bean --> request attribute
* Session scoped managed bean --> session attribute
* Application scoped managed bean --> ServletContext attribute
Therefore, using a ServletContextListener to preinitialize your
application scoped managed bean is fine ... all you have to ensure is
that the attribute name you store it under is the same as the managed
bean name. Later on, then, the managed bean facility will find that
the bean already exists, so it will not bother to create it.
Craig
> Thanks man!
>
> -Steve
>
>
>
>
> Simon Lessard wrote:
> > Hello Steve,
> >
> > Does it have to be a managed bean? You could probably chose a class
> > with static methods instead and have the context listener initialize
> > it upon application startup. You could then create a really
> > lightweight application bean extracting its data from that class with
> > static members. Forcing a bean into existence at application startup
> > can be extremelly hard as JSF VariableResolver requires a FacesContext
> > instance which is based on a request that you won't have inside the
> > listener to evaluate a value.
> >
> >
> > Regards,
> >
> > ~ Simon
> >
> > On 2/20/07, *Steve Torrefranca* <storrefranca@branders.com
> > <ma...@branders.com>> wrote:
> >
> > Hows that may I ask? A getter needs to be called in order to
> > populate a
> > List that is DB driven
> >
> > List theList = null;
> >
> > public List getTheList(){
> > if(theList == null)
> > theList = getTheListFromDB();
> >
> > return theList;
> > }
> >
> >
> >
> >
> > David Delbecq wrote:
> > > a webapp context listener
> > > En l'instant précis du 02/20/07 15:28, Steve Torrefranca
> > s'exprimait en
> > > ces termes:
> > >
> > >> what is the best way to do this?
> > >>
> > >> thank you
> > >>
> > >> -Steve
> > >>
> > >
> > >
> > >
> >
> >
>
>
Re: Loading application scoped managed beans on server startup
Posted by Steve Torrefranca <st...@branders.com>.
> Does it have to be a managed bean?
I am afraid so, though i could be wrong. The List is used by the view
as source for select Items. But yes I can use this a work around.
Thanks man!
-Steve
Simon Lessard wrote:
> Hello Steve,
>
> Does it have to be a managed bean? You could probably chose a class
> with static methods instead and have the context listener initialize
> it upon application startup. You could then create a really
> lightweight application bean extracting its data from that class with
> static members. Forcing a bean into existence at application startup
> can be extremelly hard as JSF VariableResolver requires a FacesContext
> instance which is based on a request that you won't have inside the
> listener to evaluate a value.
>
>
> Regards,
>
> ~ Simon
>
> On 2/20/07, *Steve Torrefranca* <storrefranca@branders.com
> <ma...@branders.com>> wrote:
>
> Hows that may I ask? A getter needs to be called in order to
> populate a
> List that is DB driven
>
> List theList = null;
>
> public List getTheList(){
> if(theList == null)
> theList = getTheListFromDB();
>
> return theList;
> }
>
>
>
>
> David Delbecq wrote:
> > a webapp context listener
> > En l'instant précis du 02/20/07 15:28, Steve Torrefranca
> s'exprimait en
> > ces termes:
> >
> >> what is the best way to do this?
> >>
> >> thank you
> >>
> >> -Steve
> >>
> >
> >
> >
>
>
Re: Loading application scoped managed beans on server startup
Posted by Simon Lessard <si...@gmail.com>.
Here's a mini example:
public class Cache
{
private static List myList;
public static void init(Object... someParameters)
{
myList = getTheListFromDB();
}
public static List getList()
{
return myList;
}
}
public class ApplicationBean
{
public List getList()
{
return Cache.getList();
}
}
public class MyServletContextListener implements ServletContextListener
{
public void contextInitialized(ServletContextEvent ev)
{
Cache.init(ev.getServletContext
().getInitParameter("MyDBConnectionInfoOrJNDIName"));
}
}
Regards,
~ Simon
On 2/20/07, Simon Lessard <si...@gmail.com> wrote:
>
> Hello Steve,
>
> Does it have to be a managed bean? You could probably chose a class with
> static methods instead and have the context listener initialize it upon
> application startup. You could then create a really lightweight application
> bean extracting its data from that class with static members. Forcing a bean
> into existence at application startup can be extremelly hard as JSF
> VariableResolver requires a FacesContext instance which is based on a
> request that you won't have inside the listener to evaluate a value.
>
>
> Regards,
>
> ~ Simon
>
> On 2/20/07, Steve Torrefranca <st...@branders.com> wrote:
> >
> > Hows that may I ask? A getter needs to be called in order to populate a
> > List that is DB driven
> >
> > List theList = null;
> >
> > public List getTheList(){
> > if(theList == null)
> > theList = getTheListFromDB();
> >
> > return theList;
> > }
> >
> >
> >
> >
> > David Delbecq wrote:
> > > a webapp context listener
> > > En l'instant précis du 02/20/07 15:28, Steve Torrefranca s'exprimait
> > en
> > > ces termes:
> > >
> > >> what is the best way to do this?
> > >>
> > >> thank you
> > >>
> > >> -Steve
> > >>
> > >
> > >
> > >
> >
> >
>
Re: Loading application scoped managed beans on server startup
Posted by Simon Lessard <si...@gmail.com>.
Hello Steve,
Does it have to be a managed bean? You could probably chose a class with
static methods instead and have the context listener initialize it upon
application startup. You could then create a really lightweight application
bean extracting its data from that class with static members. Forcing a bean
into existence at application startup can be extremelly hard as JSF
VariableResolver requires a FacesContext instance which is based on a
request that you won't have inside the listener to evaluate a value.
Regards,
~ Simon
On 2/20/07, Steve Torrefranca <st...@branders.com> wrote:
>
> Hows that may I ask? A getter needs to be called in order to populate a
> List that is DB driven
>
> List theList = null;
>
> public List getTheList(){
> if(theList == null)
> theList = getTheListFromDB();
>
> return theList;
> }
>
>
>
>
> David Delbecq wrote:
> > a webapp context listener
> > En l'instant précis du 02/20/07 15:28, Steve Torrefranca s'exprimait en
> > ces termes:
> >
> >> what is the best way to do this?
> >>
> >> thank you
> >>
> >> -Steve
> >>
> >
> >
> >
>
>
Re: Loading application scoped managed beans on server startup
Posted by Steve Torrefranca <st...@branders.com>.
Hows that may I ask? A getter needs to be called in order to populate a
List that is DB driven
List theList = null;
public List getTheList(){
if(theList == null)
theList = getTheListFromDB();
return theList;
}
David Delbecq wrote:
> a webapp context listener
> En l'instant précis du 02/20/07 15:28, Steve Torrefranca s'exprimait en
> ces termes:
>
>> what is the best way to do this?
>>
>> thank you
>>
>> -Steve
>>
>
>
>
Re: Loading application scoped managed beans on server startup
Posted by David Delbecq <de...@oma.be>.
a webapp context listener
En l'instant précis du 02/20/07 15:28, Steve Torrefranca s'exprimait en
ces termes:
> what is the best way to do this?
>
> thank you
>
> -Steve