如何使用@EJB、@PersistenceContext、@Inject、@Autowired 注入@FacesValidator
如何在 a 中注入 @EJB
、@PersistenceContext
、@Inject
、@AutoWired
等依赖项@FacesValidator
?在我的具体情况下,我需要通过 @AutoWired 注入 Spring 托管 bean:
@FacesValidator("emailExistValidator")
public class EmailExistValidator implements Validator {
@Autowired
private UserDao userDao;
// ...
}
但是,它没有被注入,并且仍然为 null,导致 java.util.concurrent.beans 被注入。 lang.NullPointerException。 看来@EJB、@PersistenceContext和@Inject也不起作用。
如何在验证器中注入服务依赖项以便可以访问数据库?
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;
// ...
}
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
![扫码二维码加入Web技术交流群](/public/img/jiaqun_03.jpg)
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
JSF 2.3+
如果您已经使用 JSF 2.3 或更高版本,并且想要通过例如
@EJB
、@PersistenceContext
或@Inject< 注入 CDI 支持的工件/code>,然后只需将
management=true
添加到@FacesValidator
注释即可使其受 CDI 管理。JSF 2.2 -
如果您还没有使用 JSF 2.3 或更高版本,那么您基本上需要将其设为托管 bean。使用 Spring 的 @Component 、 CDI 的
@Named
或 JSF 的@ManagedBean
而不是@FacesValidator
以便使其成为托管 bean,从而有资格进行依赖项注入。例如,假设您想使用 CDI 的
@Named
:您还需要在 EL 中通过
#{name}
将其引用为托管 Bean,而不是在 EL 中作为验证器 ID硬编码字符串。因此,对于
EJB ,
https://balusc.omnifaces.org/2011/09/communication-in-jsf-20.html
有一种解决方法,可以手动从 JNDI 获取它,另请参阅 在 @FacesConverter 中获取 @EJB 并@FacesValidator。
如果您碰巧使用 JSF 实用程序库 OmniFaces,从版本 1.6 开始,它添加了对使用
@Inject 和
@EJB
在@FacesValidator
类中,无需任何额外的配置或注释。另请参阅CDI@FacesValidator
展示示例。另请参阅:
JSF 2.3+
If you're already on JSF 2.3 or newer, and want to inject CDI-supported artifacts via e.g.
@EJB
,@PersistenceContext
or@Inject
, then simply addmanaged=true
to the@FacesValidator
annotation to make it CDI-managed.JSF 2.2-
If you're not on JSF 2.3 or newer yet, then you basically 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.E.g., assuming that you want to use CDI's
@Named
:You also need to reference it as a managed bean by
#{name}
in EL instead of as a validator ID in hardcoded string. Thus, soinstead of
or
instead of
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 JSF utility library OmniFaces, since version 1.6 it 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.See also:
如果您使用的是 Java EE 8 和/或 JSF 2.3,现在可以注入 JSF 验证器。
在 Payara Server 5.192 #badassfish 上使用 Mojarra 2.3.9.payara-p2 进行测试。
应该呈现如下内容:
在意识到需要
ConfigurationBean
之前,我把头撞在墙上大约一个小时。从文档中:,并且从此 GitHub 问题中,https://github.com/eclipse-ee4j/glassfish/issues/22094:
You can now inject into JSF validators if you're using Java EE 8 and/or JSF 2.3.
Tested using Mojarra 2.3.9.payara-p2 on Payara Server 5.192 #badassfish.
Should render something like:
I banged my head on the wall for about an hour before realizing the need for
ConfigurationBean
. From the documentation:And from this GitHub issue, https://github.com/eclipse-ee4j/glassfish/issues/22094:
我的方法是在构造函数中手动触发注入:
这是基于此博客文章: http://www.beyondjava.net/blog/integrate-jsf-2-spring-3-nicely
My approach is to trigger the injection by hand in the constructor:
This is based on this blog post: http://www.beyondjava.net/blog/integrate-jsf-2-spring-3-nicely