开发者

How do I load/initialize my class as soon as my app is started? [duplicate]

This question already has an answer here: Using special auto start servlet to initialize on startup 开发者_如何学Goand share application data (1 answer) Closed 7 years ago.

We have a Java app that runs on Websphere. I have one class that I want to load and initialize as soon as Websphere loads, or as soon as the app is reloaded within Websphere, without waiting for the first web page to be loaded. I'm pretty sure loading it in our subclass of HttpServlet is probably the wrong place. So what is the right place?


I think you want a ServletContextListener.

Before that was available, people would actually put this kind of code into a Servlet (not necessarily mapped to any url), and have that loaded on initialization. But ServletContextListener is the proper way now.

Goes into web.xml like this:

<web-app>
<listener>
    <listener-class>
     mypackage.MyContextListener
    </listener-class>
  </listener>
<servlet/>
<servlet-mapping/>
</web-app>


As Thilo mentioned ServletContextListeners are good for Web Applications.

If your application does not use the Web Components then you could possibly be looking at WebSphere specific extensions too - StartUp Beans.

http://publib.boulder.ibm.com/infocenter/wasinfo/v6r0/index.jsp?topic=/com.ibm.websphere.express.doc/info/exp/asyncbns/tasks/tasb_confstb.html

HTH Manglu

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜