有没有办法只在 Tomcat/Wildfly/Glassfish 启动时运行方法/类? [英] Is there a way to run a method/class only on Tomcat/Wildfly/Glassfish startup?

查看:29
本文介绍了有没有办法只在 Tomcat/Wildfly/Glassfish 启动时运行方法/类?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在 Tomcat 启动时删除临时文件,传递到包含临时文件的文件夹在 applicationContext.xml 中.

I need to remove temp files on Tomcat startup, the pass to a folder which contains temp files is in applicationContext.xml.

有没有办法只在 Tomcat 启动时运行一个方法/类?

Is there a way to run a method/class only on Tomcat startup?

推荐答案

您可以编写一个 ServletContextListenercontextInitialized() 方法调用您的方法.您在 web.xml 中将侦听器附加到您的 web 应用程序,例如

You could write a ServletContextListener which calls your method from the contextInitialized() method. You attach the listener to your webapp in web.xml, e.g.

<listener>
   <listener-class>my.Listener</listener-class>
</listener>

package my;

public class Listener implements javax.servlet.ServletContextListener {

   public void contextInitialized(ServletContext context) {
      MyOtherClass.callMe();
   }
}

严格来说,这只会在 webapp 启动时运行一次,而不是在 Tomcat 启动时运行,但这可能是一样的.

Strictly speaking, this is only run once on webapp startup, rather than Tomcat startup, but that may amount to the same thing.

这篇关于有没有办法只在 Tomcat/Wildfly/Glassfish 启动时运行方法/类?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆