Keywords: Spring Boot | Embedded Container | Dependency Configuration | Web Application | Immediate Shutdown
Abstract: This article provides an in-depth analysis of the common causes behind Spring Boot applications shutting down immediately after startup, with a focus on the issue of missing embedded web container dependencies. Through detailed code examples and configuration explanations, it demonstrates how to properly configure Spring Boot Starter Web dependencies to ensure continuous application operation and web request handling. The article also includes configuration methods for both Maven and Gradle build tools to help developers quickly identify and resolve similar issues.
Problem Phenomenon Analysis
During Spring Boot application development, many beginners encounter the issue where the application shuts down immediately after starting. From the provided log information, we can see that the application successfully starts and completes initialization in 7.396 seconds, but then immediately begins the shutdown process. This phenomenon typically indicates that the application lacks necessary components to maintain a running state.
Root Cause Investigation
The core reason for Spring Boot application automatic shutdown is the absence of an embedded web container. When the application detects no available web server in the classpath, Spring Boot considers it a non-web application and exits normally after completing initialization. In the provided example, the configuration in build.gradle excludes Tomcat dependency:
dependencies {
compile("org.springframework.boot:spring-boot-starter-web") {
exclude module: "spring-boot-starter-tomcat"
}
}This configuration explicitly excludes the spring-boot-starter-tomcat module, preventing the application from starting an embedded web server.
Solution Implementation
Gradle Configuration Correction
For projects using Gradle build tool, the correct dependency configuration should include the complete spring-boot-starter-web:
dependencies {
implementation 'org.springframework.boot:spring-boot-starter-web'
}For older versions of Gradle, the compile configuration can be used:
dependencies {
compile 'org.springframework.boot:spring-boot-starter-web'
}Maven Configuration Approach
If using Maven build tool, add the following dependency in pom.xml:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>Technical Principles Deep Dive
Spring Boot detects dependencies in the classpath through its auto-configuration mechanism. When spring-boot-starter-web is present, Spring Boot automatically configures the embedded Tomcat server and starts the web application context. This starter includes Spring MVC, Tomcat, and other dependencies required for web development.
After correcting the configuration, the application startup log shows that AnnotationConfigEmbeddedWebApplicationContext is created, indicating that the embedded web application context has been successfully initialized, allowing the application to run continuously and handle HTTP requests.
Optimized Code Examples
Below is the complete corrected controller example:
package hello;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
@EnableAutoConfiguration
public class SampleController {
@RequestMapping("/")
@ResponseBody
String home() {
return "Hello World!";
}
public static void main(String[] args) {
SpringApplication.run(SampleController.class, args);
}
}This example demonstrates the basic structure of a Spring Boot web application, including controller, request mapping, and auto-configuration.
System Design Considerations
At the system design level, understanding the importance of dependency management is crucial. Proper dependency configuration not only affects application functionality but also influences application behavior and performance characteristics. Developers should deeply understand the components included in each starter and their roles, avoiding unnecessary dependency exclusions.
Best Practice Recommendations
1. Always use spring-boot-starter-parent or corresponding BOM to manage dependency versions
2. Avoid manually excluding core components from starters unless there are clear alternative solutions
3. Regularly check dependency trees to ensure no conflicts or missing dependencies
4. Use spring-boot-devtools in development environment for hot deployment to improve development efficiency
Conclusion
The issue of Spring Boot applications shutting down immediately after startup typically stems from missing embedded web container dependencies. By correctly configuring spring-boot-starter-web dependencies, applications can be ensured to run continuously and handle web requests. Understanding Spring Boot's auto-configuration mechanism and starter工作原理 helps developers quickly identify and resolve similar issues, improving development efficiency.