You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@myfaces.apache.org by mathieu fabre <ma...@gmail.com> on 2008/03/20 10:18:39 UTC

question about managed bean

 Hi,

I use JSF and MyFace in a webapp. I use some managed bean ( declared in the
face-config.xml)
I would like to know if it's possible to create a managed bean at the
begining (like a tag <load-on-startup> we can use in servlet)
and not only when requested for the first time...

Thanks,

Mat

RE: question about managed bean

Posted by "Kito D. Mann" <km...@virtua.com>.
Mathieu,

I'd just configure them in a ServletContextListener. Just create the objects
and store them in the Servlet context (application scope). Spring makes
sense, but it's overkill just for this bit of functionality. If, however,
you really want the features that Spring provides (as a full-fledged
dependency injection container), then consider using Spring for your managed
beans.

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
Kito D. Mann - Author, JavaServer Faces in Action
http://www.virtua.com - JSF/Java EE consulting, training, and mentoring
http://www.JSFCentral.com - JavaServer Faces FAQ, news, and info

> -----Original Message-----
> From: simon.kitching@chello.at [mailto:simon.kitching@chello.at]
> Sent: Thursday, March 20, 2008 6:38 AM
> To: MyFaces Development
> Subject: Re: question about managed bean
> 
> mathieu fabre schrieb:
> > Hi,
> >
> > I use JSF and MyFace in a webapp. I use some managed bean ( declared
> > in the
> > face-config.xml)
> > I would like to know if it's possible to create a managed bean at the
> > begining (like a tag <load-on-startup> we can use in servlet)
> > and not only when requested for the first time...
> 
> Not with the standard JSF managed-beans functionality.
> 
> I presume that this is an application-scope bean (anything else would
> not make sense to create on startup). In this case, you could use a
> servlet or servlet-context-listener to just add an entry into the
> standard application scope for the webapp; a JSF EL expression will
> find
> it there if it exists even though it is not defined as a managed-bean
> in
> the faces-config.xml file.
> 
> If you really want to define it in the faces-config.xml file (eg to
> configure it via dependency injection) then it is possible by having a
> ServletContextListener that sets up a FacesContext instance, then loads
> the bean manually. But that is not trivial.
> 
> But the easiest solution of all would be to use Spring to define your
> managed beans rather than the JSF built-in managed-beans configuration.
> This gives a whole lot of really useful features, one of which is the
> ability to create beans on startup. There is a page on the myfaces wiki
> about this:
>   http://wiki.apache.org/myfaces/JSF_With_Spring
> 
> Regards,
> Simon


Re: question about managed bean

Posted by mathieu fabre <ma...@gmail.com>.
Indeed, it is the application-scope bean.

Thanks you for your answer which is clear and complete.
I will try the third solution.

Thanks again,

Mat

2008/3/20, simon.kitching@chello.at <si...@chello.at>:
>
> mathieu fabre schrieb:
>
> > Hi,
> >
> > I use JSF and MyFace in a webapp. I use some managed bean ( declared
> > in the
> > face-config.xml)
> > I would like to know if it's possible to create a managed bean at the
> > begining (like a tag <load-on-startup> we can use in servlet)
> > and not only when requested for the first time...
>
>
> Not with the standard JSF managed-beans functionality.
>
> I presume that this is an application-scope bean (anything else would
> not make sense to create on startup). In this case, you could use a
> servlet or servlet-context-listener to just add an entry into the
> standard application scope for the webapp; a JSF EL expression will find
> it there if it exists even though it is not defined as a managed-bean in
> the faces-config.xml file.
>
> If you really want to define it in the faces-config.xml file (eg to
> configure it via dependency injection) then it is possible by having a
> ServletContextListener that sets up a FacesContext instance, then loads
> the bean manually. But that is not trivial.
>
> But the easiest solution of all would be to use Spring to define your
> managed beans rather than the JSF built-in managed-beans configuration.
> This gives a whole lot of really useful features, one of which is the
> ability to create beans on startup. There is a page on the myfaces wiki
> about this:
>   http://wiki.apache.org/myfaces/JSF_With_Spring
>
> Regards,
>
> Simon
>
>

Re: question about managed bean

Posted by "simon.kitching@chello.at" <si...@chello.at>.
mathieu fabre schrieb:
> Hi,
>
> I use JSF and MyFace in a webapp. I use some managed bean ( declared
> in the
> face-config.xml)
> I would like to know if it's possible to create a managed bean at the
> begining (like a tag <load-on-startup> we can use in servlet)
> and not only when requested for the first time...

Not with the standard JSF managed-beans functionality.

I presume that this is an application-scope bean (anything else would
not make sense to create on startup). In this case, you could use a
servlet or servlet-context-listener to just add an entry into the
standard application scope for the webapp; a JSF EL expression will find
it there if it exists even though it is not defined as a managed-bean in
the faces-config.xml file.

If you really want to define it in the faces-config.xml file (eg to
configure it via dependency injection) then it is possible by having a
ServletContextListener that sets up a FacesContext instance, then loads
the bean manually. But that is not trivial.

But the easiest solution of all would be to use Spring to define your
managed beans rather than the JSF built-in managed-beans configuration.
This gives a whole lot of really useful features, one of which is the
ability to create beans on startup. There is a page on the myfaces wiki
about this:
  http://wiki.apache.org/myfaces/JSF_With_Spring

Regards,
Simon