如何初始化Java FacesServlet [英] How can I initialize a Java FacesServlet
问题描述
我需要在FacesServlet启动时运行一些代码,但是当FacesServlet被声明为final时,我无法扩展它并覆盖init()方法。
I need to run some code when the FacesServlet starts, but as FacesServlet is declared final I can not extend it and overwrite the init() method.
特别是,我希望在开发和测试期间,在hibernate删除并创建数据模型后,将一些数据写入数据库。
In particular, I want to write some data to the database during development and testing, after hibernate has dropped and created the datamodel.
有没有办法配置Faces来运行某些方法,例如在faces-config.xml中?
或者最好创建一个执行初始化的单例bean吗?
Is there a way to configure Faces to run some method, e.g. in faces-config.xml? Or is it best to create a singleton bean that does the initialization?
推荐答案
@ManagedBean(eager=true)
@ApplicationScoped
public class App {
@PostConstruct
public void startup() {
// ...
}
@PreDestroy
public void shutdown() {
// ...
}
}
(类和方法名称实际上无关紧要,你可以免费使用选择,所有关于注释)
这是有保证的在启动 FacesServlet
后构建,因此 FacesContext
将随时可用必要。这与其他答案所建议的 ServletContextListener
相反。
This is guaranteed to be constructed after the startup of the FacesServlet
, so the FacesContext
will be available whenever necessary. This in contrary to the ServletContextListener
as suggested by the other answer.
这篇关于如何初始化Java FacesServlet的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!