You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by Simon Kitching <si...@rhe.co.nz> on 2007/05/01 23:11:33 UTC

Re: Accessing registered beans

Francisco Passos wrote:
> Hello there.
> 
> I'd like to know if there's a way to get a list of the names of the 
> managed beans that are declared in faces-config.xml. I'm trying to get 
> my beans to implement an interface and call specific methods upon a 
> particular phase.
> 
> However, even if I implement a PhaseListener, I still have to do 
> something like this:
> 
> MyBean1 bean = (MyBean1) 
> FacesContext.getCurrentInstance().getApplication().getVariableResolver().resolveVariable(FacesContext.getCurrentInstance 
> (), "myBean1");
> 
> bean.callDesiredMethod();
> 
> Only I'd like to do this for any of my beans in the scope... is this 
> possible?

I don't know of any easy way to access the list of declared managed 
beans. There certainly isn't a portable way to do this. One possible 
solution would be to register your own VariableResolver (there is a 
standard way to do this) and have that simply call the normal 
VariableResolver then cache the result somewhere you can access. This 
isn't quite the same, but close.

It *is* trivial to get a list of all the objects in the 
request/session/application scopes using the standard Servlet API. Is 
this not sufficient?

Note that the resolveVariable method does the following:
(1) look in request scope for the specified name
(2) look in session scope
(3) look in application scope
(4) look for a managed bean with that name, and if found create an 
instance and add it to the appropriate scope.

Steps 1-3 are easy to implement yourself. Actually, you probably don't 
want (3) anyway as the PhaseListener executes for each request.

Do you really want step 4 to happen, ie to force every declared managed 
bean to be created? Normally managed beans are created only when 
actually referenced (see 4 above).

See class org.apache.myfaces.el.VariableResolverImpl in the myfaces 
source for the full details; it isn't too complicated.


Regards,

Simon