验证器不支持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);
}
答案 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.2(spec issue 763)之外支持@ManagedBean
以外的JSF工件中的依赖项注入。