TechnologyBeginnerJavaSpringBootSpringFramework

What Really Happens When You Run a Spring Boot Application?

What Really Happens When You Run a Spring Boot Application?

Have you ever wondered what happens behind the scenes when you execute this single line of Java code?

SpringApplication.run(MyApplication.class, args);

It looks simple, right?

But behind that one line, Spring Boot performs several important operations to bring your application to life.

Let's break down the Spring Boot startup lifecycle in 8 simple steps!

1. Main Class — The Entry Point

Every Spring Boot application starts with the Java main() method.

Java8 lines
@SpringBootApplication
public class MyApplication {
    public static void main(String[] args) {
        SpringApplication.run(
            MyApplication.class, args
        );
    }
}

The @SpringBootApplication annotation enables key Spring Boot features, including component scanning and auto-configuration.

2. SpringApplication.run() — Bootstrapping

This is where Spring Boot takes control.

It prepares the application environment, processes configuration, and starts initializing the Spring application.

Think of it as turning on the engine of your application. ??

3 ApplicationContext — The Spring Container

Spring creates an ApplicationContext, which acts as the application's IoC (Inversion of Control) container.

Its responsibility is to:

Manage Spring beans

Resolve dependencies

Handle bean lifecycles

Instead of manually creating and connecting every object, Spring manages them for you.

4. Component Scanning — Discovering Your Classes

Spring searches configured packages for classes annotated with:

  • @Component — General Spring-managed component
  • @Service — Business logic layer
  • @Repository — Data access layer
  • @Controller / @RestController — Web layer

For example:

Java6 lines
@Service
public class UserService {
    public String getUser() {
        return "Hello Spring Boot!";
    }
}

Spring discovers UserService and registers its bean definition.

**Important By default, component scanning starts from the package containing your main application class and includes its subpackages.

5. Bean Creation & Dependency Injection

Spring creates the necessary objects (beans) and connects their dependencies.

For example:

Java9 lines
@RestController
public class UserController {

    private final UserService userService;

    public UserController(UserService userService) {
        this.userService = userService;
    }
}

Notice something interesting?

We never wrote:

new UserService()

Spring automatically provides the required UserService bean through constructor-based dependency injection.

This promotes loose coupling, testability, and maintainable code.

6. Auto-Configuration — Smart Configuration

Spring Boot examines your dependencies, application properties, and existing beans to configure the application automatically.

For example:

Spring Web dependency = Configures Spring MVC infrastructure

Spring Data JPA dependency = Configures JPA infrastructure when requirements are met

Spring Security dependency = Applies default security configuration when applicable

This is why Spring Boot requires significantly less manual configuration than traditional Spring applications.

7. Embedded Web Server — Starting the Server

For a typical Spring MVC application using spring-boot-starter-web, Spring Boot starts an embedded Tomcat server by default.

For example:

Tomcat started on port 8080

You don't need to deploy your application manually to an external Tomcat server.

Spring Boot handles it for you!

8. Application Ready — Ready to Serve Requests

Once the application context is initialized, startup runners have completed, and Spring Boot publishes ApplicationReadyEvent, the application startup process is complete.

Your REST endpoints can now handle incoming requests.

For example:

GET http://localhost:8080/users

The request flows through:

Client > Tomcat > DispatcherServlet > Controller > Service > Repository > Database

The response then travels back to the client.

Your Spring Boot application is running!


The Big Picture

Code24 lines
       Java main()
            ?
   SpringApplication.run()
            ?
   Prepare Environment
            ?
   Create ApplicationContext
            ?
   Process Configuration
   & Component Scanning
            ?
   Auto-Configuration
            ?
   Create & Inject Beans
            ?
   Initialize Web Server
            ?
   Complete Context Refresh
            ?
   Run Startup Runners
            ?
   ApplicationReadyEvent
            ?
   Application Ready

Note: This is a simplified conceptual flow. Component scanning, auto-configuration, bean initialization, and web server startup are interconnected parts of context initialization, not always separate sequential phases.

Key Takeaway

Spring Boot is much more than a framework for building REST APIs.

Understanding what happens during startup helps you:

Debug NoSuchBeanDefinitionException

Understand BeanCreationException

Fix dependency injection issues

Troubleshoot auto-configuration problems

Design better Spring Boot applications

The better you understand how Spring Boot works internally, the easier it becomes to build, debug, and maintain real-world applications.

JavaSpringBootSpringFrameworkBackendDevelopmentSoftwareEngineeringDependencyInjectionJavaDeveloperMicroservices
S

Written by Simon Temesgen

Sharing practical knowledge with the MelkaTech community.

Join the discussion

Ask a question or share what you learned. Comments are reviewed before appearing.