You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@myfaces.apache.org by David Tashima <da...@gmail.com> on 2005/06/16 20:34:14 UTC

Initializing a managed bean (Spring integration)

Hey everyone,

I am integrating Spring into my JSF application, and had a question
about the lifecycle of a JSF managed bean.

I have a JSF managed bean that depends on a Spring managed bean. The
problem is that I need to do some initialization on the bean after it
has been created and *after the Spring Bean has been injected*.

Is there any way to define an init() method that gets invoked by JSF
after the properties have been set?

I am using the OOTB JSF integration piece that comes w/ Spring (not
the Spring-JSF project stuff... perhaps my problem is addressed
there?)

Thanks!
Dave

Re: Initializing a managed bean (Spring integration)

Posted by Cenk Çivici <ce...@gmail.com>.
Spring DelegatingVariableResolver class can inject Spring Managed
Beans to JSF Managed Beans. Havent tried this in myfaces but might
work.

Cenk Çivici
www.jroller.com/page/cenkcivici

1. Add the resolver to faces-config.xml

<application>

<variable-resolver>org.springframework.web.jsf.DelegatingVariableResolver</variable-resolver>

</application>

2.  A business delegate object "ReservationService" which is
configured in Spring xml as follows.

 <bean id="reservationService" class="tbs.service.ReservationService">
...
 </bean>

3. If you want to have this bean injected to your jsf managed bean
during initialization you need to do the following.

<managed-bean>
   <managed-bean-name>pc_reservationPage</managed-bean-name>
   <managed-bean-class>tbs.web.ReservationPage</managed-bean-class>
   <managed-bean-scope>request</managed-bean-scope>
   <managed-property>
     <property-name>reservationService</property-name>
     <value>#{reservationService}</value>
   </managed-property>
</managed-bean>

4. Last thing you need to have a setter in your jsf managed bean like

public ReservationPage  {

protected IReservationService reservationService;

public setReservastionService(IReservationService reservationService) {

     this.reservationService = reservationService; 

}

...

...

}



On 6/16/05, David Tashima <da...@gmail.com> wrote:
> Hey everyone,
> 
> I am integrating Spring into my JSF application, and had a question
> about the lifecycle of a JSF managed bean.
> 
> I have a JSF managed bean that depends on a Spring managed bean. The
> problem is that I need to do some initialization on the bean after it
> has been created and *after the Spring Bean has been injected*.
> 
> Is there any way to define an init() method that gets invoked by JSF
> after the properties have been set?
> 
> I am using the OOTB JSF integration piece that comes w/ Spring (not
> the Spring-JSF project stuff... perhaps my problem is addressed
> there?)
> 
> Thanks!
> Dave
>

Re: Initializing a managed bean (Spring integration)

Posted by David Tashima <da...@gmail.com>.
Yes, I was thinking about that, but it seemed a bit hacky to me -
especially since it depends on the order that the properties are set.

Actually, you're right - having Spring manage it works, since it has
application scope anyway.

So if the scope had to be something more transient than application
scope, I would have to use the jsf-spring package?

-Dave

On 6/16/05, Duncan Mills <du...@oracle.com> wrote:
> David,  Could you not do the initialision in the setter that is being
> used to inject the Spring managed value?
> The other option would be to let Spring manage this object instead as
> you do have a lot more flexibilty there - however option 1 seems simpler
> if it would work in this situation..
> 
> Duncan Mills
> www.groundside.com/blog
> 
> David Tashima wrote:
> 
> >Hey everyone,
> >
> >I am integrating Spring into my JSF application, and had a question
> >about the lifecycle of a JSF managed bean.I have a JSF managed bean that depends on a Spring managed bean. The
> >problem is that I need to do some initialization on the bean after it
> >has been created and *after the Spring Bean has been injected*.
> >
> >Is there any way to define an init() method that gets invoked by JSF
> >after the properties have been set?
> >
> >I am using the OOTB JSF integration piece that comes w/ Spring (not
> >the Spring-JSF project stuff... perhaps my problem is addressed
> >there?)
> >
> >Thanks!
> >Dave
> >
> >
> 
>

Re: Initializing a managed bean (Spring integration)

Posted by Duncan Mills <du...@oracle.com>.
David,  Could you not do the initialision in the setter that is being 
used to inject the Spring managed value?
The other option would be to let Spring manage this object instead as 
you do have a lot more flexibilty there - however option 1 seems simpler 
if it would work in this situation..

Duncan Mills
www.groundside.com/blog

David Tashima wrote:

>Hey everyone,
>
>I am integrating Spring into my JSF application, and had a question
>about the lifecycle of a JSF managed bean.I have a JSF managed bean that depends on a Spring managed bean. The
>problem is that I need to do some initialization on the bean after it
>has been created and *after the Spring Bean has been injected*.
>
>Is there any way to define an init() method that gets invoked by JSF
>after the properties have been set?
>
>I am using the OOTB JSF integration piece that comes w/ Spring (not
>the Spring-JSF project stuff... perhaps my problem is addressed
>there?)
>
>Thanks!
>Dave
>  
>