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

查看:164
本文介绍了有没有办法只在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?

推荐答案

你可以写一个 ServletContextListener contextInitialized()方法调用您的方法。您将侦听器附加到web.xml中的webapp,例如。

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天全站免登陆