如何配置启动托管bean?

时间:2012-07-13 16:52:21

标签: jsf startup managed-bean

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

1 个答案:

答案 0 :(得分:17)

在带有CDI的JSF中,observe initializationapplication 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,可以使用@Eager进行简化。

@Named
@Eager
@ApplicationScoped
public class App {

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

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

在JSF 2.2中,使用now deprecated javax.faces.bean注释,使用application scoped初始化managed beaneagerly

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

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

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