@PostConstruct无法在@FacesValidator中运行

时间:2012-05-28 19:24:37

标签: jsf validation postconstruct

验证器不支持post构造注释吗?

我有一个应用程序范围的jndi servicelocator bean,我将其作为托管属性注入到我的验证器中。

@ManagedProperty(value = "#{jndiServiceLocatorBean}")
private final JndiServiceLocatorBean jndiServiceLocatorBean = null;

永远不会调用post构造注释方法来初始化我必需的远程bean,所以我的远程bean仍然是null。

private UserBeanRemote userBeanRemote = null;

@PostConstruct
public void postConstruct()
{
    this.userBeanRemote = (UserBeanRemote) this.jndiServiceLocatorBean.getRemoteBean(UserBeanRemote.class);
}

1 个答案:

答案 0 :(得分:3)

仅当Validator注释为@ManagedBean@Named而不是@FacesValidator时才有效。

只需使用普通构造函数即可。

@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);
    }

    // ...
}

计划在JSF 2.2spec issue 763)之外支持@ManagedBean以外的JSF工件中的依赖项注入。

另见: