3

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);
}
djmj
  • 5,579
  • 5
  • 54
  • 92

1 Answers1

3

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:

Community
  • 1
  • 1
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • 1
    Jsf 2.2 will make many things much easier! So many good changes :) As a JavaEE newbie it is all confusing what works and what works not and why and why not. – djmj May 28 '12 at 19:42
  • It was really useful to know that the support for this is in plan for 2.2 Thanks BalusC – Oh Chin Boon Aug 14 '13 at 09:36
  • @Chin: unfortunately, `@FacesConverter`/`@FacesValidator` have to wait for 2.3. OmniFaces 1.6 will come with solution for JSF 2.0/2.1/2.2. – BalusC Aug 14 '13 at 10:24