Using @ApplicationScoped @Named @Eager
, my @EJB
-注入@Stateless
beans 未正确实例化并评估为null
.
我有一个@ApplicationScoped @ManagedBean(eager=true)
它被用来安排一些工作。一些@Stateless
豆子被注入使用@EJB
注释,效果很好。
在转向 CDI 注释时,我添加了OmniFaces@Eager http://showcase.omnifaces.org/cdi/Eager注释作为替代@ManagedBean(eager=true)
标准 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
.
在日志中,出现以下警告:
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
and @Schedule
注释。
我怎样才能正确注射@Stateless
豆子在一个@Named
将在应用程序启动时实例化的应用程序作用域 bean?