我正处于“操作方法”阶段,Apache CXF想知道是否有办法在服务器启动时调用方法。
它将类似于JSFWeb 应用程序,当我使用@ApplicationScoped托管 bean 时eager=true:当容器启动时,带注释的类被实例化,我可以从它的构造函数中调用我想要的任何东西。
有什么帮助吗?
因此,如果您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();
您可以设置任意数量的属性,以便在整个应用程序范围内可用。