What is the main method of a WAR file?

I recently unpacked a java spring application again to become a WAR file for deployment to tomcat. After some testing, I noticed that public static void main(String[] args)it is not running. Some necessary initialization of my application is done in main. Is there something like a method mainin a WAR file? What is the appropriate place in the WAR file to start some initialization?

+4
source share
3 answers

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

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

This method is executed, regardless of whether I run my application autonomously or in tomcat.

. bean ? Init spring ( )

+1

web.xml :

<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
    }
}
+9

, web.xml, .

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

ServletContextListener

public class WebAppContext implements ServletContextListener {

public void contextInitialized(ServletContextEvent servletContextEvent) {
// Do your processing that you are trying to do in main method.
}
+3

All Articles