Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do I need main method if I develop web app as war using Spring Boot?

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);
    }

}
like image 592
brain storm Avatar asked Apr 21 '15 20:04

brain storm


People also ask

Why does a spring boot application need a main () method?

2 Answers. Show activity on this post. Method main is an entry point for standalone applications, so if you want to use spring boot standalone application, usually (if not always) packaged into a JAR - then yes, you should use main method.

Does spring boot need war files?

In Spring Boot, the new final executable JAR file with embedded server solution may not suitable in all production environments, especially the deployment team (a team with good knowledge of server optimization and monitoring skills, but lack of, the development experience), they want full control of the server, and ...

Can we develop web application using Spring boot?

Project Dependencies. Spring Boot Starter Web provides all the dependencies and the auto configuration needed to develop web applications. We should use the first dependency.

What is the main benefit of use spring boot instead of spring?

Spring Boot advantages include; No need for XML configuration, no need for WAR files, no need for POM, less source code, easy ember Tomcat, Jetty, etc.


2 Answers

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.

like image 88
Naymesh Mistry Avatar answered Sep 18 '22 14:09

Naymesh Mistry


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/

like image 44
Faraj Farook Avatar answered Sep 20 '22 14:09

Faraj Farook