@PostConstruct not working in @FacesValidator
-
11-06-2021 - |
Question
Is the post construct annotation not supported in validators?
I have a application scoped jndi servicelocator bean which I inject as a managed property into my validator.
@ManagedProperty(value = "#{jndiServiceLocatorBean}")
private final JndiServiceLocatorBean jndiServiceLocatorBean = null;
The post construct annotated method to initialize my necessary remote bean is never invoked and so my remote bean remains null.
private UserBeanRemote userBeanRemote = null;
@PostConstruct
public void postConstruct()
{
this.userBeanRemote = (UserBeanRemote) this.jndiServiceLocatorBean.getRemoteBean(UserBeanRemote.class);
}
Solution
It works only if the Validator
is annotated as a @ManagedBean
or @Named
instead of @FacesValidator
.
Just use the normal constructor instead.
@FacesValidator("fooValidator")
public class FooValidator implements Validator {
private UserBeanRemote userBeanRemote;
public FooValidator() {
FacesContext context = FacesContext.getCurrentInstance();
JndiServiceLocatorBean jndiServiceLocatorBean = context.getApplication().evaluateExpressionGet(context, "#{jndiServiceLocatorBean}", JndiServiceLocatorBean.class);
this.userBeanRemote = (UserBeanRemote) jndiServiceLocatorBean.getRemoteBean(UserBeanRemote.class);
}
// ...
}
Support for dependency injection in JSF artifacts other than @ManagedBean
is planned for JSF 2.2 (spec issue 763).
See also:
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow