在CDI中等效于@ManagedBean(eager = true) [英] What is the equivalent of @ManagedBean(eager=true) in CDI
问题描述
众所周知,建议不要使用javax.enterprise.context
中的注释,而应使用javax.enterprise.context
中的注释.
As we all know that it is recommended to use annotations from javax.enterprise.context
instead of javax.faces.bean
as they are getting deprecated.
并且我们都发现带有eager="true"
且由javax.faces.bean
注释为@ApplicationScoped
的ManagedBeans并具有@PostConstruct
方法对于进行Web应用程序初始化非常有用,例如:从文件系统读取属性,初始化数据库连接等. ..
And we all found ManagedBeans with eager="true"
annotated with @ApplicationScoped
from javax.faces.bean
and having a @PostConstruct
method are very useful to do web application initialization e.g: read properties from file system, initialize database connections, etc...
示例:
Example :
import javax.faces.bean.ApplicationScoped;
import javax.faces.bean.ManagedBean;
import javax.annotation.PostConstruct;
@ApplicationScoped
@ManagedBean(eager=true)
public class someBean{
@PostConstruct
public void init(){
//Do all needed application initialization.
}
...
}
我想知道的是,如果我使用javax.enterprise.context
中的注释,如何获得相同的行为.
What I want to know is how can I get the same behavior if I used annotations from javax.enterprise.context
.
注意:
javax.ejb
中的@Startup
批注将有助于运行该代码,但仅在应用程序服务器 启动 时在部署Webapp时运行.
Note:
@Startup
annotation from javax.ejb
will help to run that code but only at the moment of deployment of the webapp when the application server Starts.
推荐答案
CDI或JSF不提供此功能.您可以使用自定义CDI限定词和 ServletContextListener
挂接webapp.
This is not provided by CDI or JSF. You could homegrow your own with a custom CDI qualifier and a ServletContextListener
to hook on webapp start.
@Qualifier
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Eager {
//
}
@WebListener
public class EagerListener implements ServletContextListener{
private static final AnnotationLiteral<Eager> EAGER_ANNOTATION = new AnnotationLiteral<Eager>() {
private static final long serialVersionUID = 1L;
};
@Override
public void contextInitialized(ServletContextEvent event) {
CDI.current().select(EAGER_ANNOTATION).forEach(bean -> bean.toString());
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// NOOP.
}
}
(注意:toString()
触发延迟实例化)
(note: toString()
triggers lazy instantiation)
import com.example.Eager;
import javax.enterprise.context.ApplicationScoped;
@Eager
@ApplicationScoped
public class YourEagerApplicationScopedBean {
@PostConstruct
public void init() {
System.out.println("Application scoped init!");
}
}
对于现有库,只有JSF实用程序库 OmniFaces 提供
As to existing libraries, only JSF utility library OmniFaces offers @Eager
out the box.
import org.omnifaces.cdi.Eager;
import javax.enterprise.context.ApplicationScoped;
@Eager
@ApplicationScoped
public class YourEagerApplicationScopedBean {
@PostConstruct
public void init() {
System.out.println("Application scoped init!");
}
}
@SessionScoped
,@ViewScoped
和@RequestScoped
上也得到支持.
不管采用哪种方法,唯一的缺点是FacesContext
在构造bean时不可用.但这不是什么大问题,使用CDI,您可以直接直接@Inject
感兴趣的工件,例如ServletContext
或HttpSession
.
Regardless of the approach, the only disadvantage is that FacesContext
isn't available at the moment the bean is constructed. But that shouldn't be a big problem, with CDI you can simply directly @Inject
artifacts of interest such as ServletContext
or HttpSession
.
这篇关于在CDI中等效于@ManagedBean(eager = true)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!