问题描述
我想听听JSF应用程序是通过普通Servlet Web应用程序中的ServletContextListener
启动还是停止的.我该如何实现?
I would like to listen if a JSF application is started or stopped like as possible with a ServletContextListener
in a plain Servlet web application. How can I achieve this?
推荐答案
您可以使用 @ApplicationScoped
@ManagedBean
会被初始化,并使用 @PostConstruct
和 @PreDestroy
.
You can use an @ApplicationScoped
@ManagedBean
which is eagerly initialized and annotate the desired startup/shutdown hook methods with @PostConstruct
and @PreDestroy
respectively.
所以:
@ManagedBean(eager=true)
@ApplicationScoped
public class App {
@PostConstruct
public void init() {
// ...
}
@PreDestroy
public void destroy() {
// ...
}
}
请注意,这不并不意味着您不能在JSF Web应用程序中使用ServletContextListener
. JSF建立在Servlet API之上,因此,您可以继续使用它.使用新的Servlet 3.0 @WebListener
批注,也可以配置它而无需web.xml
:
Please note that this does not mean that you can't use a ServletContextListener
in a JSF webapp. JSF is built on top of the Servlet API which thus means that you could just continue using it. With the new Servlet 3.0 @WebListener
annotation, it's also possible to configure it without need for web.xml
:
@WebListener
public class Config implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent event) {
// ...
}
@Override
public void contextDestroyed(ServletContextEvent event) {
// ...
}
}
这篇关于JSF中是否有类似ServletContextListener的东西?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!