如何在@FacesValidator中注入@EJB,@PersistenceContext,@Inject,@Autowired [英] How to inject in @FacesValidator with @EJB, @PersistenceContext, @Inject, @Autowired
问题描述
如何注入一个依赖关系,如 @EJB
, @PersistenceContext
, @Inject
, @AutoWired
等在 @FacesValidator
中?在我的具体情况下,我需要通过 @AutoWired
注入一个Spring管理的bean:
How can I inject a dependency like @EJB
, @PersistenceContext
, @Inject
, @AutoWired
, etc in a @FacesValidator
? In my specific case I need to inject a Spring managed bean via @AutoWired
:
@FacesValidator("emailExistValidator")
public class EmailExistValidator implements Validator {
@Autowired
private UserDao userDao;
// ...
}
然而,它没有被注入,它仍然是 null
,导致 java.lang.NullPointerException
。
似乎 @EJB
, @PersistenceContext
和 @Inject
也不起作用。
However, it didn't get injected and it remains null
, resulting in java.lang.NullPointerException
.
It seems that @EJB
, @PersistenceContext
and @Inject
also doesn't work.
如何在验证器中注入服务依赖关系,以便我可以访问数据库?
How do I inject a service dependency in my validator so that I can access the DB?
推荐答案
@FacesValidator
不受注射容器管理。您需要使其成为托管bean。使用Spring的 @Component
,CDI的 @Named
或JSF的 @ManagedBean
,而不是 @FacesValidator
,以使其成为受管理的bean,从而符合依赖注入资格。
The @FacesValidator
isn't managed by the injection container. You need to make it a managed bean. Use Spring's @Component
, CDI's @Named
or JSF's @ManagedBean
instead of @FacesValidator
in order to make it a managed bean and thus eligible for dependency injection.
例如,假设你想使用JSF的 @ManagedBean
:
E.g., assuming that you want to use JSF's @ManagedBean
:
@ManagedBean
@RequestScoped
public class EmailExistValidator implements Validator {
// ...
}
您还需要通过EL中的#{name}
将其引用为托管bean,而不是作为验证器ID硬编码字符串。因此,所以
You also need to reference it as a managed bean by #{name}
in EL instead of as a validator ID in hardcoded string. Thus, so
<h:inputText ... validator="#{emailExistValidator.validate}" />
或
<f:validator binding="#{emailExistValidator}" />
而不是
<h:inputText ... validator="emailExistValidator" />
或
<f:validator validatorId="emailExistValidator" />
这确实很尴尬。 JSF的人已经确认了这个令人尴尬的监督,他们将使 @FacesValidator
(和 @FacesConverter
)合格的注射目标在即将到来的JSF 2.2 2.3中,另见 JSF规范问题763 。对于EJB,通过从JNDI手动抓取它可以解决这个问题,另见获取@EJB @FacesConverter和@FacesValidator 。如果您恰好使用CDI扩展程序 MyFaces CODI ,那么您也可以通过将
This is indeed awkward. The JSF guys have confirmed this embarrassing oversight and they will make the @FacesValidator
(and @FacesConverter
) an eligible injection target in upcoming JSF 2.2 2.3, see also JSF spec issue 763. For EJBs there's a workaround by manually grabbing it from JNDI, see also Getting an @EJB in @FacesConverter and @FacesValidator. If you happen to use the CDI extension MyFaces CODI, then you can also solve it by putting @Advanced
annotation on the class.
- CDI Injection into a FacesConverter
- What's new in JSF 2.2 - Injection
更新:如果您碰巧使用JSF实用程序库 OmniFaces ,因为1.6版本为使用 @Inject
和 @EJB
添加了透明支持一个 @FacesValidator
类,没有任何其他配置或注释。另请参见 CDI @FacesValidator
展示示例。
Update: if you happen to use JSF utility library OmniFaces, since version 1.6 is adds transparent support for using @Inject
and @EJB
in a @FacesValidator
class without any additional configuration or annotations. See also the CDI @FacesValidator
showcase example.
这篇关于如何在@FacesValidator中注入@EJB,@PersistenceContext,@Inject,@Autowired的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!