users@javaserverfaces.java.net

RE: Re: Initial page loading: the managed bean doesn't inject the EJB when called in the constructor.

From: Todd Patrick <Todd.Patrick_at_dtn.com>
Date: Wed, 5 Sep 2007 15:49:16 -0500

Ryan:

When a JSF page loads initially, the managed bean is created as soon as
the page has an EL Expression reference to a field within the managed
bean. So in this case, the managed bean would be created after the
RESTORE_VIEW phase id?

When a postback has occurred, the bean has been created, so any method
annotated with @PostConstruct will *not* be called. But you can do a
bunch of fancy stuff by accessing the JSF Lifecycle phases within a JSF
1.2 beforePhase or afterPhase event?

Am I on target or am I still way off?

:)

Thanks,

--Todd


-----Original Message-----
From: Ryan.Lubke_at_Sun.COM [mailto:Ryan.Lubke_at_Sun.COM]
Sent: Wednesday, September 05, 2007 3:38 PM
To: users_at_javaserverfaces.dev.java.net
Subject: Re: Initial page loading: the managed bean doesn't inject the
EJB when called in the constructor.

Todd Patrick wrote:
> Ryan:
>
> Two notes; to alleviate any confusions that I may have - when you said

> "Assign any managed-properties declared..." - are you referring to the

> managed bean's properties that are defined as fields with related
> getters/setters?
>
> Or are you referring to the managed-property element(s) defined in the

> faces-config.xml, such as:
>
Managed properties as defined in the faces-config.xml.
> <managed-bean>
> <description>Client Search Bean</description>
> <managed-bean-name>clientSearch</managed-bean-name>
> <managed-bean-class>com.dtn.petro2.petroadmin.client.ClientSearchBean<
> /m
> anaged-bean-class>
> <managed-bean-scope>session</managed-bean-scope>
> <managed-property>
> <property-name>visit</property-name>
> <value>#{sessionScope.visit}</value>
> </managed-property>
> </managed-bean>
>
> In the five step order below, where can I find information or how does

> that order relate to the JSF Lifecycle?
>
Managed beans will be created and put into scope the first time they are
referenced.
This could potentially happen anywhere.

> Thanks,
>
> --Todd
>
>
> -----Original Message-----
> From: Ryan.Lubke_at_Sun.COM [mailto:Ryan.Lubke_at_Sun.COM]
> Sent: Wednesday, September 05, 2007 3:12 PM
> To: users_at_javaserverfaces.dev.java.net
> Subject: Re: Initial page loading: the managed bean doesn't inject the

> EJB when called in the constructor.
>
> Todd Patrick wrote:
>
>> Ryan:
>>
>> OK, with that said, I had always believed that a method annotated
>> with
>>
>
>
>> @PostConstruct would only execute after the injection of an EJB or a
>> resource?
>>
>> Or does a method annotated with @PostConstruct - once - run directly
>> after the Constructor call when the managed bean is initially
created?
>>
>>
> Here is the order:
>
> 1. New the bean declared using the default constructor
> 2. Inject any |_at_Resource| declarations
> 3. Assign any managed-properties declared, cascading this process
if
> new beans are referenced
> 4. Call |_at_PostConstruct| on the bean if declared
> 5. Store the bean in the correct scope and return
>
>
>
>
>> Thanks,
>>
>> --Todd
>>
>> -----Original Message-----
>> From: Ryan.Lubke_at_Sun.COM [mailto:Ryan.Lubke_at_Sun.COM]
>> Sent: Wednesday, September 05, 2007 2:33 PM
>> To: users_at_javaserverfaces.dev.java.net
>> Subject: Re: Initial page loading: the managed bean doesn't inject
>> the
>>
>
>
>> EJB when called in the constructor.
>>
>> Todd Patrick wrote:
>>
>>
>>> Sun Java System Application Server Platform Edition 9.0_01 (build
>>> b02-p01)
>>> Sun's JavaServer Faces implementation (1.2_04-b10-p01) NetBeans
>>> 5.5.1
>>>
>>> When a page loads for the first time, the managed bean doesn't know
>>> about the injected EJB inside the constructor:
>>>
>>> public class RoutingRuleBean extends DeviceBasicBean implements
>>> Serializable { @EJB private RouteRuleSessionLocal
>>> routeRuleSessionLocal; ....
>>> public RoutingRuleBean() {
>>> ...
>>> if(!ObjectUtils.equals(this.selectedRouteDevcRowDetails, null)) {
>>> DeviceDetail selectedDeviceDetail =
>>> routeRuleSessionLocal.getRouteDevcDetail(this.selectedRouteDevcRowDe
>>> t
>>> a
>>> il
>>> s.getDeviceKey());
>>> this.device.setValue(selectedDeviceDetail);
>>> }
>>>
>>>
>>> In the snippet above, the field "routeRuleSessionLocal" holding the
>>> injected EJB is null inside the RoutingRuleBean constructor.
>>>
>>> So, the JSF Lifecycle is probably looking like - when a page loads
>>> for
>>>
>>>
>>
>>
>>> the first time:
>>>
>>> RESTORE_VIEW
>>>
>>> [constructor]
>>>
>>> RENDER_RESPONSE
>>>
>>>
>>> Thus, the injected EJB isn't called until the managed bean is
>>> completely initialized and pass the RENDER_RESPONSE phase.
>>>
>>> Looking at the Java EE API, I know of the PostConstruct annotation,
>>> but is there something that I could use before the constructor is
>>>
>>>
>> called?
>>
>>
>>>
>>>
>>>
>> Not in this case. The injection occurs on the actual instance, so
>> the
>>
>
>
>> ctor is always called first.
>> @PostConstuct is your best bet.
>>
>>
>>> Thanks,
>>>
>>> --Todd
>>>
>>>
>>> -----------------------------------------
>>> NOTICE: This email message is for the sole use of the intended
>>> recipient(s) and may contain confidential and privileged
information.
>>>
>
>
>>> Any unauthorized use, disclosure or distribution is prohibited. If
>>> you
>>>
>>>
>>
>>
>>> are not the intended recipient, please contact the sender by reply
>>> email and destroy all copies of the original message.
>>>
>>> --------------------------------------------------------------------
>>> -
>>> To unsubscribe, e-mail:
>>> users-unsubscribe_at_javaserverfaces.dev.java.net
>>> For additional commands, e-mail:
>>> users-help_at_javaserverfaces.dev.java.net
>>>
>>>
>>>
>>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
>> users-unsubscribe_at_javaserverfaces.dev.java.net
>> For additional commands, e-mail:
>> users-help_at_javaserverfaces.dev.java.net
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail:
>> users-unsubscribe_at_javaserverfaces.dev.java.net
>> For additional commands, e-mail:
>> users-help_at_javaserverfaces.dev.java.net
>>
>>
>>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_javaserverfaces.dev.java.net
> For additional commands, e-mail:
> users-help_at_javaserverfaces.dev.java.net
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_javaserverfaces.dev.java.net
> For additional commands, e-mail:
> users-help_at_javaserverfaces.dev.java.net
>
>

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe_at_javaserverfaces.dev.java.net
For additional commands, e-mail: users-help_at_javaserverfaces.dev.java.net