Accessing injected dependency in managed bean constructor causes NullPointerException

Injection can only take place after construction simply because before construction there’s no eligible injection target. Imagine the following fictive example: UserInfoBean userInfoBean; UserDao userDao = new UserDao(); userInfoBean.setDao(userDao); // Injection takes place. userInfoBean = new UserInfoBean(); // Constructor invoked. This is technically simply not possible. In reality the following is what is happening: UserInfoBean … Read more

ViewParam vs @ManagedProperty(value = “#{param.id}”)

<f:viewParam>: Sets the value during update model values phase only (since it extends UIInput). The set value is not available during @PostConstruct, so you need an additional <f:event type=”preRenderView” listener=”#{bean.init}” /> inside the <f:metadata> to do initialization/preloading based on the set values. Since JSF 2.2 you could use <f:viewAction> for that instead. Allows for nested … Read more