19

I am developing web app using Spring Boot. My typical deployment is generating war and place it in webapps folder in Tomcat directory.

I noticed with SpringBoot, I will need a main method. I am wondering why this is needed. If there is a way to avoid it, what would that be?

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

    public static void main(String[] args) throws Exception {
        SpringApplication.run(Application.class, args);
    }

}
6
  • 1
    I don't think you need a main method if you want to deploy as war. What happens if you remove it?
    – JB Nizet
    Apr 21, 2015 at 20:28
  • Yeah, you don't need it. You can just remove it if it is only ever just a WAR file you are building.
    – rhinds
    Apr 21, 2015 at 20:29
  • The crucial part of the documentations is "(...) you update your application’s main class to extend SpringBootServletInitializer"; if you search the rest of that page for occurrences of "public class Application" you'll see that they simply refer to the previous shape of that class.
    – kryger
    Apr 21, 2015 at 21:14
  • @JBNizet: the build fails if I remove main method. I have a post here on this: stackoverflow.com/questions/29831953/… Apr 23, 2015 at 18:50
  • 1
    @kryger: please see my comment above Apr 23, 2015 at 18:52

3 Answers 3

20

Main method is not required for the typical deployment scenario of building a war and placing it in webapps folder of Tomcat. All you need is:

public class Application extends SpringBootServletInitializer {

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

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.

3
  • +1. thanks for the info. btw what does application.sources(Application.class) do? what is its sole purpose? Is it necessary or can it be avoided? Apr 22, 2015 at 17:31
  • 1
    As per the JavaDoc of the method, it adds more sources (configuration classes and components) for Spring to scan for context initialisation of this application. Apr 23, 2015 at 1:32
  • I tried without main method and it is failing: Here it is : stackoverflow.com/questions/29831953/… Apr 23, 2015 at 20:14
5

To run in a separate web container

You don't need the main method, all you need is to do is to extend SpringBootServletInitializer as Kryger mentioned.

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

To run in the command line as a standalone application

Here you need the main method, so that you can run it using java -jar from the command line.

public class Application {
    public static void main(String[] args){
        ConfigurableApplicationContext context = SpringApplication.run(Application.class, args);
    }
....
....

Source: https://spring.io/guides/gs/convert-jar-to-war/

2
0

In Spring Boot one will basically need three things :

1) use the @SpringBootApplication annotation

2) extend SpringBootServletInitializer

3) overwrite the configure method as shown above

and that's it !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.