30 - Understanding the main() Method in Spring Boot
- 23 - Spring Boot Architecture Overview
- 24 - Key Spring Boot Annotations Explained
- 25 - Auto-Configuration: How Spring Boot Does Magic
- 26 - Spring Boot Starters – The Complete Guide
- 27 - Creating a Basic Spring Boot Application (Hands-on)
- 28 - Embedded Web Servers in Spring Boot (Tomcat, Jetty, Netty)
- 29 - Spring Boot Application Startup Process
- 30 - Understanding the main() Method in Spring BootCurrent
- 31 - Best Practices for Spring Boot Applications
- 32 - application.properties vs application.yml in Spring Boot
- 33 - Profiles & Environment Configuration in Spring Boot (@Profile)
- 34 - Spring Boot Logging – Complete Guide
- 35 - Spring Boot DevTools – Hot Reloading & Developer Productivity
- 36 - Using Spring Boot Actuator (Health, Metrics, Insights)
At the center of every Spring Boot application lies a simple main() method.
While it looks like plain Java, this method triggers a powerful and complex bootstrapping process.
In this post, we’ll zoom in on the main() method and understand what really happens when a Spring Boot app starts.
1. The main() Method in Spring Boot
A typical Spring Boot application starts like this:
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
This is a standard Java entry point, but the call to SpringApplication.run() is where Spring Boot takes over.
2. What Is SpringApplication?
SpringApplication is a helper class provided by Spring Boot that:
- Bootstraps the Spring application
- Creates the ApplicationContext
- Triggers auto-configuration
- Starts the embedded web server
- Publishes lifecycle events
It acts as the orchestrator of the startup process.
3. What Happens Inside SpringApplication.run()?
Internally, SpringApplication.run() performs these steps:
- Creates a
SpringApplicationinstance - Determines application type (web, reactive, non-web)
- Prepares the Environment
- Creates the ApplicationContext
- Loads bean definitions
- Applies auto-configuration
- Refreshes the context
- Starts the embedded server
- Publishes startup events
All of this is triggered by a single method call.
4. Application Type Detection
Spring Boot automatically detects the application type based on the classpath:
- Servlet-based → Spring MVC
- Reactive → Spring WebFlux
- Non-web → CLI applications
This detection decides which ApplicationContext implementation to use.
5. Customizing SpringApplication
Instead of calling run() directly, you can customize behavior.
public static void main(String[] args) {
SpringApplication app = new SpringApplication(DemoApplication.class);
app.setBannerMode(Banner.Mode.OFF);
app.run(args);
}
Common customizations include:
- Disabling banner
- Setting default profiles
- Adding listeners
- Customizing startup behavior
6. CommandLineRunner & ApplicationRunner
Spring Boot provides hooks to run code after startup.
6.1 CommandLineRunner
@Component
public class StartupRunner implements CommandLineRunner {
@Override
public void run(String... args) {
System.out.println("Application started!");
}
}
6.2 ApplicationRunner
@Component
public class AppRunner implements ApplicationRunner {
@Override
public void run(ApplicationArguments args) {
System.out.println("Application ready!");
}
}
Both execute after the context is fully initialized.
7. Passing Command-Line Arguments
Arguments passed to main() are available to Spring:
java -jar app.jar --server.port=9090
Access them using:
ApplicationArguments@Value- Environment
This enables runtime customization without code changes.
8. Common Mistakes
- Placing
main()outside the root package - Heavy logic inside
main() - Ignoring startup arguments
- Multiple
@SpringBootApplicationclasses
The main() method should remain light and focused.
9. Best Practices
- Keep
main()minimal - Use SpringApplication customization sparingly
- Use runners for startup logic
- Prefer configuration over code
- Let Spring Boot manage the lifecycle
10. Summary
main()is the Java entry pointSpringApplication.run()boots the entire app- Application type is auto-detected
- Embedded server starts automatically
- Customization is possible but optional
Understanding this method gives you confidence in how Spring Boot applications start and run.
What's Next?
Next post:
31 - Best Practices for Spring Boot Applications
We’ll cover architectural, coding, and configuration best practices for real-world projects.
