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