Spring Boot
SpringApplication
Java
main method
application startup

SpringApplication.run main method

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

Introduction

SpringApplication.run is the bootstrap entry for most Spring Boot applications. It wires up the application context, starts auto configuration, and launches embedded servers when applicable. Understanding this method helps you debug startup failures and customize boot behavior safely.

What Happens During SpringApplication.run

At a high level, Spring Boot performs these steps:

  1. Create and configure SpringApplication instance.
  2. Prepare environment and property sources.
  3. Create application context based on application type.
  4. Register beans and auto configuration.
  5. Refresh context and run lifecycle callbacks.
  6. Start web server for servlet or reactive applications.

A minimal entry point looks like this.

java
1import org.springframework.boot.SpringApplication;
2import org.springframework.boot.autoconfigure.SpringBootApplication;
3
4@SpringBootApplication
5public class DemoApplication {
6    public static void main(String[] args) {
7        SpringApplication.run(DemoApplication.class, args);
8    }
9}

This single call hides substantial initialization work.

Customizing Startup Behavior

You can create a SpringApplication object manually to adjust startup behavior.

java
1import org.springframework.boot.Banner;
2import org.springframework.boot.SpringApplication;
3import org.springframework.boot.WebApplicationType;
4
5public class CustomMain {
6    public static void main(String[] args) {
7        SpringApplication app = new SpringApplication(DemoApplication.class);
8        app.setBannerMode(Banner.Mode.OFF);
9        app.setWebApplicationType(WebApplicationType.SERVLET);
10        app.run(args);
11    }
12}

This pattern is useful when you need explicit control for tools, batch jobs, or tests.

Lifecycle Hooks You Should Know

Two hooks are commonly used during startup customization:

  • ApplicationRunner and CommandLineRunner for post context startup logic.
  • ApplicationListener for observing lifecycle events.
java
1import org.springframework.boot.ApplicationArguments;
2import org.springframework.boot.ApplicationRunner;
3import org.springframework.stereotype.Component;
4
5@Component
6public class StartupRunner implements ApplicationRunner {
7    @Override
8    public void run(ApplicationArguments args) {
9        System.out.println("Application started with option names: " + args.getOptionNames());
10    }
11}

Use these hooks for initialization tasks that require fully wired dependencies.

Debugging Startup Issues

When startup fails, the first actionable source is the condition evaluation report and stack trace near the root cause. Common failures include missing configuration, port conflicts, circular dependencies, and bean creation errors.

Practical debugging steps:

  1. Enable debug logging for auto configuration report.
  2. Verify active profiles and property sources.
  3. Check whether mandatory environment variables are set.
  4. Isolate failing bean by temporary exclusion or conditional configuration.

Understanding run lifecycle makes these checks faster because you know where failure likely occurred.

Production Considerations

Keep startup logic lightweight. Heavy remote calls in startup hooks can delay readiness and cause orchestration timeouts in container platforms. Prefer deferred initialization for non critical tasks.

Also ensure liveness and readiness probes align with application lifecycle. A service that reports ready before essential warmup can fail under immediate traffic.

Configuration Precedence And Startup Order

Spring Boot merges configuration from multiple sources, including properties files, environment variables, command line arguments, and profile specific files. Understanding precedence is critical when startup behavior does not match expectations.

Command line arguments generally have high priority, which can override packaged defaults unexpectedly in container orchestration environments. During incidents, print effective configuration for key fields such as database URL, active profile, and server port.

Startup order also affects custom initializers and listeners. If a component depends on environment values, ensure it runs after environment preparation. If it depends on fully initialized beans, prefer runner interfaces after context refresh. Choosing the wrong hook phase is a frequent source of null references and missing configuration at startup.

Common Pitfalls

  • Placing business workflows directly inside main instead of lifecycle hooks.
  • Running expensive network operations during startup without timeout controls.
  • Assuming all profiles and properties are loaded as expected without verification.
  • Ignoring conditional auto configuration logs during troubleshooting.
  • Over customizing bootstrapping before understanding default behavior.

Summary

  • SpringApplication.run orchestrates full application bootstrap.
  • It prepares environment, context, bean wiring, and server startup.
  • Manual SpringApplication construction enables controlled customization.
  • Lifecycle hooks are the right place for post startup initialization.
  • Startup reliability depends on clear configuration and lightweight boot logic.

Related reading
Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

Interview Questions practice on Codemia

Over 8,000 real interview questions from top companies, searchable by company and role.

Browse interview questions

All Rights Reserved.