6

Recently, I've reapackaged my java spring application to become a WAR file for deployment in tomcat. After some testing I noticed, that public static void main(String[] args) is not executed. Some necessary initialization of my application is done in main. Is there something like a main method in a WAR file? What is the appropiate place in a WAR file to run some initialization?

user1785730
  • 2,326
  • 2
  • 19
  • 39
  • 2
    You can specify in the `web.xml` file to execute a class on startup... – brso05 Apr 20 '15 at 12:49
  • See also: http://stackoverflow.com/questions/8686507/how-to-add-a-hook-to-the-application-context-initialization-event – JimmyB Apr 20 '15 at 12:55
  • This looks specific to Spring Boot - please update the tags, both answers posted at the moment don't take this fact into account. – kryger Apr 20 '15 at 13:58

3 Answers3

11

You can add a listener to your web.xml file:

<listener>
    <description>Application startup and shutdown events</description>
    <display-name>Test</display-name>
    <listener-class>com.package.package.StartClass</listener-class>
</listener>

public class StartClass implements ServletContextListener {

    @Override
    public void contextDestroyed(ServletContextEvent servletContextEvent) {
         //Context destroyed code here
    }

    @Override
    public void contextInitialized(ServletContextEvent servletContextEvent)
    {
        //Context initialized code here
    }
}
brso05
  • 12,634
  • 1
  • 17
  • 37
  • You may want to change the `display-name` :-) – Aaron Digulla Apr 20 '15 at 12:52
  • @AaronDigulla you mean he may want to change the display name lol this is just an example... – brso05 Apr 20 '15 at 12:54
  • I don't have a web.xml file, but do that with SpringBootServletInitializer. – user1785730 Apr 20 '15 at 12:56
  • 1
    @user1785730: Annotate your class with `@WebListener`. See http://docs.oracle.com/javaee/6/api/javax/servlet/annotation/WebListener.html – Aaron Digulla Apr 20 '15 at 14:20
  • I work on a project, where web.xml has three listeners, and all of them do not belong to the project files - they are about some Spring classes. So, it is not a listener that points to the start class. – Gangnus Nov 05 '20 at 19:48
4

Well, You will have to create a listener in your web.xml that will be invoked by container at the time of startup.

<listener>
    <listener-class>com.rdv.example.WebAppContext</listener-class>
</listener>

And this class will be implementing ServletContextListener

public class WebAppContext implements ServletContextListener {

public void contextInitialized(ServletContextEvent servletContextEvent) {
// Do your processing that you are trying to do in main method.
}
Raja
  • 811
  • 9
  • 22
1

I've found another way, that is independent of spring and tomcat: The @PostConstruct annotation. In code:

@PostConstruct
public void init() {
    // initialization code goes here
}

This method gets executed whether I run my application standalone or in tomcat.

For more information see How to call a method after bean initialization is complete? or Init method in Spring Controller (annotation version)

Community
  • 1
  • 1
user1785730
  • 2,326
  • 2
  • 19
  • 39