users@javaserverfaces.java.net

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

From: Ryan Lubke <Ryan.Lubke_at_Sun.COM>
Date: Wed, 05 Sep 2007 12:33:27 -0700

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.selectedRouteDevcRowDetail
> 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
>
>