[Resolved] Spring Boot app shuts down automatically at startup
This post will discuss how to resolve the Spring Boot application shut down immediately after startup.
We know that a Spring Boot web app needs an embedded servlet container on the classpath. The app will shut down if it doesn’t find any of the embedded servlet containers. To resolve the unexpected shut down during startup, we need to obtain a fully configured instance using the appropriate Starter. Currently, Spring Boot includes support for embedded Tomcat, Jetty, and Undertow servers.
Solution #1
To add the necessary dependencies, edit your pom.xml and add the spring-boot-starter-web dependency immediately below the parent section:
|
1 2 3 4 5 6 |
<dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies> |
The spring-boot-starter-web dependency is a starter for building web apps, including RESTful applications, using Spring MVC. It uses the Tomcat web server as the default embedded container by including spring-boot-starter-tomcat, but you can use the spring-boot-starter-jetty or spring-boot-starter-undertow instead, which are a starter for using Jetty and Undertow as the embedded servlet container.
Solution #2
To build a war file that is both executable and deployable into an external container, we mark the embedded container dependencies as provided, as shown in the following example:
|
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
<packaging>war</packaging> <!-- … --> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <scope>provided</scope> </dependency> <!-- … --> </dependencies> |
This ensures that the embedded servlet container does not interfere with the servlet container to which the war file is deployed. But marking the embedded servlet container dependency as provided will cause our app to crash in a local environment. To resolve this, edit your pom.xml and comment-out the provided scope for the spring-boot-starter-tomcat artifact, as shown below:
|
1 2 3 4 5 |
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <!--<scope>provided</scope>--> </dependency> |
That’s all about resolving the Spring Boot application shut down immediately after startup.
Thanks for reading.
To share your code in the comments, please use our online compiler that supports C, C++, Java, Python, JavaScript, C#, PHP, and many more popular programming languages.
Like us? Refer us to your friends and support our growth. Happy coding :)