我正处于Apache CXF
的“操作方法”阶段,并且想知道在服务器启动时是否有办法调用方法。
当我使用带有JSF
的{{1}}托管bean时,它类似于@ApplicationScoped
Web应用程序:当容器启动时,带注释的类被实例化,我可以调用无论我想从它的构造函数。
任何帮助?
答案 0 :(得分:4)
因此,如果您使用CXF Servlet
来提供Web Service
请求,那么您可以创建ServletContextListener
并在部署或服务器启动时调用contextInitialized
方法该应用程序已经部署。
为此,创建将实现ServletContextListener
:
public class YourContextListener implements ServletContextListener {
@Override
public void contextInitialized(ServletContextEvent sce) {
//This method is called by the container on start up
}
@Override
public void contextDestroyed(ServletContextEvent sce) {
}
}
然后在web.xml
:
<listener>
<listener-class>your.package.YourContextListener</listener-class>
</listener>
在contextInitialized
方法中,您可以使用以下方法获取servlet上下文:
ServletContext context = sce.getServletContext();
您可以设置任意数量的属性,以便在整个应用程序范围内可用。