在Glassfish Web部署之后执行代码

Jai*_*ung 1 java deployment web-services glassfish web-deployment

我正在尝试在Glassfish上运行java Web服务.有一些初始化代码可以设置一些变量并从Glassfish环境本身检索一些信息.我在@WebService类的静态初始化程序中有这个代码,但是这个代码看起来太早了,它在部署WebService端点后立即运行,而我需要在整个Web服务成功部署后运行它.

我尝试将代码移动到WebService类的构造函数中,然后该代码仅在我进入Tester Web页面并发送一些数据以运行Web方法时运行.

有没有办法在整个Web服务部署完成后立即设置一些初始化代码?

Gar*_*ero 6

选项1:在Glassfish中,您拥有Lifecycle模块

选项2: 您还可以编写在加载上下文时触发的ServletContextListener:

public class MyServlet implements ServletContextListener {

  public void contextInitialized(ServletContextEvent e) {
         // implementation code
  }

  public void contextDestroyed(ServletContextEvent e) {
         // implementation code
  }
}
Run Code Online (Sandbox Code Playgroud)

参考: