【问题标题】:@PostConstruct not working in @FacesValidator@PostConstruct 在 @FacesValidator 中不起作用
【发布时间】:2012-06-03 02:36:30
【问题描述】:

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

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

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

用于初始化我必要的远程 bean 的 post 构造注释方法永远不会被调用,因此我的远程 bean 保持为空。

private UserBeanRemote userBeanRemote = null;

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

【问题讨论】:

    标签: jsf validation postconstruct


    【解决方案1】:

    仅当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.2 (spec issue 763) 中支持 @ManagedBean 以外的 JSF 工件中的依赖项注入。

    另见:

    【讨论】:

    • Jsf 2.2 将使许多事情变得更容易!这么多好的变化 :) 作为一个 JavaEE 新手,什么可行,什么不可行,为什么不可行。
    • 知道在 2.2 计划中对此的支持非常有用,谢谢 BalusC
    • @Chin:不幸的是,@FacesConverter/@FacesValidator 必须等待 2.3。 OmniFaces 1.6 将附带 JSF 2.0/2.1/2.2 的解决方案。
    猜你喜欢
    • 2017-05-26
    • 2011-11-24
    • 1970-01-01
    • 2018-04-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-05
    • 2017-06-01
    相关资源
    最近更新 更多