Spring Boot application without main method

Do you always need a main method in a springboot application?

No

Use cases

Typically, for developing web applications to be used with Tomcat. The generated war file is placed in webapps folder in Tomcat directory.

Implementation

To use Springboot for those scenarios, one will basically need three things :

  1. use the @SpringBootApplication annotation
  2. extend SpringBootServletInitializer
  3. overwrite the configure method as shown above
@SpringBootApplication
public class Application extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
}

You can also leave it empty

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

}

However, if you want to be able to launch the app from within an IDE (e.g. with Eclipse’s Run As -> Java Application) while developing or build an executable jar or a war that can run standalone with Spring Boot’s embedded tomcat by just java -jar myapp.war command, an entry point class with a main method might be helpful.


Links to this note