在CDI中等效于@ManagedBean(eager = true) [英] What is the equivalent of @ManagedBean(eager=true) in CDI

查看:174
本文介绍了在CDI中等效于@ManagedBean(eager = true)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

众所周知,建议不要使用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感兴趣的工件,例如ServletContextHttpSession.

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆