如何配置启动受管bean? [英] How to configure a start up managed bean?

查看:92
本文介绍了如何配置启动受管bean?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望在托管应用程序加载时在我的JSF Web应用程序中在启动时在内部运行托管bean.如何编写此类并在Glassfish中进行配置?

I want a managed bean to run internally on start up in my JSF web application when the application loads. How can I write this class and configure in Glassfish?

推荐答案

在具有CDI的JSF中,应用程序的nofollow noreferrer>初始化范围.

In JSF with CDI, observe the initialization of the application scope.

@Named
@ApplicationScoped
public class App {

    public void startup(@Observes @Initialized(ApplicationScoped.class) Object context) {
        // ...
    }

    public void shutdown(@Observes @Destroyed(ApplicationScoped.class) Object context) {
        // ...
    }

}

手头有 OmniFaces 时,可以使用

When having OmniFaces at hands, this can be simplified with @Eager.

@Named
@Eager
@ApplicationScoped
public class App {

    @PostConstruct
    public void startup() {
        // ...
    }

    @PreDestroy
    public void shutdown() {
        // ...
    }
}

在JSF 2.2中,现在已弃用 javax.faces.bean注释,请使用范围限定的应用程序 托管bean ,它是

In JSF 2.2- with the now deprecated javax.faces.bean annotations, use an application scoped managed bean which is eagerly initialized.

@ManagedBean(eager=true)
@ApplicationScoped
public class App {

    @PostConstruct
    public void startup() {
        // ...
    }

    @PreDestroy
    public void shutdown() {
        // ...
    }
}

这篇关于如何配置启动受管bean?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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