在OmniFaces @Eager bean中注入@EBB会导致“严重:没有用于注入org.omnifaces.cdi.eager.EagerBeansRepository的有效EE环境” [英] Injecting @EJB in OmniFaces @Eager bean causes "Severe: No valid EE environment for injection of org.omnifaces.cdi.eager.EagerBeansRepository"
问题描述
使用 @ApplicationScoped @Named @Eager
,我的 @EJB
被注入 @Stateless
bean未正确实例化,并评估为 null
。
Using @ApplicationScoped @Named @Eager
, my @EJB
-injected @Stateless
beans are not properly instantiated and evaluate to null
.
我有一个 @ApplicationScoped @ManagedBean(eager = true)
,用于安排几个作业。一些 @Stateless
bean是使用 @EJB
注释注入的,而且工作正常。
I had an @ApplicationScoped @ManagedBean(eager=true)
that was used to schedule a few jobs. Some @Stateless
beans were injected using @EJB
annotation, and that worked fine.
在转到CDI注释时,我添加了 OmniFaces @渴望
注释替代标准CDI中缺少的 @ManagedBean(eager = true)
In the move to CDI annotations, I added the OmniFaces @Eager
annotation as substitute for @ManagedBean(eager=true)
which is missing in standard CDI:
import javax.annotation.PostConstruct;
import javax.annotation.PreDestroy;
import javax.ejb.EJB;
import javax.enterprise.context.ApplicationScoped;
import javax.inject.Inject;
import javax.inject.Named;
import org.omnifaces.cdi.Eager;
@Named
@ApplicationScoped
@Eager
public class MyScheduler implements Serializable {
@EJB
private MyService myService;
@Inject
private MyNamedBean myNamedBean;
@PostConstruct
public void init() {
setupSchedulers();
}
@PreDestroy
public void destroy() {
destroySchedulers();
}
//...
}
使用此安装程序,应用程序启动时正确调用 @PostConstruct
方法(尽管在上下文初始化之前似乎运行),但是然后 myService
评估为
null
。
Using this setup, the @PostConstruct
method is correctly called on application startup (though it seems to run even before the context is initialized), but then myService
evaluates to null
.
在日志中,会出现以下警告:
In the log, the following warnings appear:
Severe: No valid EE environment for injection of org.omnifaces.cdi.eager.EagerBeansRepository
Severe: No valid EE environment for injection of my.package.MyScheduler
Info: Initializing Mojarra 2.2.8 ( 20140814-1418 https://svn.java.net/svn/mojarra~svn/tags/2.2.8@13507) for context '/tagific'
由于我需要从其他的bean访问这个bean,所以我无法使用 @Singleton
和 @Schedule
注释。
Since I need to access this bean from other ones, I couldn't use the @Singleton
and @Schedule
annotations.
如何正确注入 @Stateless
bean在 @Named
应用程序作用域bean应用程序启动时可以实例化?
How could I properly inject @Stateless
beans in an @Named
applications scoped bean that would be instantiated on application startup?
推荐答案
这看起来像GlassFish中的初始化排序错误。 @Eager @ApplicationScoped
运行在一个 ServletContextListener
中。显然在这一点上,GlassFish并没有准备好注射EJB。这种结构在例如WildFly。
This looks like an initialization ordering bug in GlassFish. The @Eager @ApplicationScoped
runs in a ServletContextListener
. Apparently at that point GlassFish hasn't EJBs ready for injection. This construct works in e.g. WildFly.
但是,在CDI的名称中,在整个Java EE中统一了各种不同的注入方式,您也可以使用 @Inject
而不是 @EJB
。 CDI代理能够进一步委托 @Stateless
实例。
However, in CDI's name of unifying various different depency injection approaches throughout Java EE, you can also just use @Inject
instead of @EJB
. The CDI proxy is capable of delegating further to the right @Stateless
instance.
@Inject
private MyService myService;
您还可以在EJB中使用 @Inject
本身,但到目前为止(Java EE 7),它还不支持例如的自引用 @Asynchronous
方法。因为你仍然坚持 @EJB
。
You can also use @Inject
inside EJBs itself, but as of now (Java EE 7) it doesn't yet support self-referencing for e.g. @Asynchronous
methods. For that you have still to stick to @EJB
.
也就是说,您是否知道Oracle停止了GlassFish的商业支持,最好不要将其用于生产环境?另见此博客。
That said, are you aware that Oracle stopped commercial support on GlassFish and that you'd better not use it for production environments? See also this blog.
这篇关于在OmniFaces @Eager bean中注入@EBB会导致“严重:没有用于注入org.omnifaces.cdi.eager.EagerBeansRepository的有效EE环境”的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!