How does a Spring application get started?

A Spring application starts by creating and initializing the Spring Container, also known as the ApplicationContext. The container is responsible for scanning classes, creating beans, injecting dependencies, loading configurations, and managing the complete lifecycle of the application. In Spring Boot, this process is automatically initiated through the SpringApplication.run() method.

Key Points: • SpringApplication.run() is the entry point of a Spring Boot application. • The ApplicationContext is created to manage all Spring beans. • Component scanning discovers and registers Spring-managed classes. • Dependency injection is performed automatically during startup. • For web applications, an embedded server such as Tomcat is started automatically.

Application Startup Flow

main() | SpringApplication.run() | Create ApplicationContext | Component Scanning | Bean Creation | Dependency Injection | Auto-Configuration | Embedded Server Startup | Application Ready

Step 1: Application Entry Point

Every Spring Boot application starts from the main method.

Code Example:

@SpringBootApplication
public class EmployeeApplication {

    public static void main(String[] args) {

        SpringApplication.run(
                EmployeeApplication.class,
                args);
    }
}

The run() method triggers the complete startup process.

Step 2: Create ApplicationContext

Spring creates an ApplicationContext.

The ApplicationContext:

• Creates beans • Manages dependencies • Handles bean lifecycle • Provides dependency injection

It acts as the central container of the application.

Step 3: Component Scanning

Spring scans packages starting from the package containing the main class.

It looks for annotations such as:

• @Component • @Service • @Repository • @Controller • @RestController

Example:

@Service
public class EmployeeService {

}

Spring automatically detects and registers this class as a bean.

Step 4: Bean Creation

After discovering components, Spring creates bean instances.

Example:

@Service
public class EmployeeService {

}

@Repository
public class EmployeeRepository {

}

Both classes become managed beans inside the container.

Step 5: Dependency Injection

Spring resolves dependencies and injects them automatically.

Example:

@Service
public class EmployeeService {

    private final EmployeeRepository repository;

    public EmployeeService(
            EmployeeRepository repository) {

        this.repository = repository;
    }
}

Spring creates EmployeeRepository first and injects it into EmployeeService.

Step 6: Auto-Configuration

Spring Boot automatically configures components based on project dependencies.

Example:

If the project contains:

spring-boot-starter-data-jpa

Spring Boot automatically configures:

• DataSource • EntityManager • Transaction Manager

without requiring manual setup.

Step 7: Load Configuration Properties

Spring loads configuration values from:

• application.properties • application.yml • Environment variables • Command-line arguments

Example:

server.port=9090

spring.datasource.url=jdbc:mysql://localhost/testdb

These values are applied during startup.

Step 8: Start Embedded Server

For web applications, Spring Boot starts an embedded server.

Supported Servers:

• Tomcat (Default) • Jetty • Undertow

Example:

spring-boot-starter-web

automatically starts Tomcat.

The application becomes accessible through:

http://localhost:8080

Step 9: Application Ready

After all beans are initialized and the server starts, the application is ready to process requests.

Example: Suppose an Employee Management application contains:

• EmployeeController • EmployeeService • EmployeeRepository

During startup:

1. SpringApplication.run() is called. 2. ApplicationContext is created. 3. Components are scanned. 4. Beans are instantiated. 5. Dependencies are injected. 6. Auto-configuration is applied. 7. Tomcat starts. 8. REST endpoints become available.

Benefits of the Spring Startup Process

• Automatic bean management • Reduced configuration effort • Dependency injection support • Automatic server startup • Faster development • Easier maintenance

Real-World Example

In a Spring Boot microservice:

PaymentService Application

When the application starts:

• Spring creates all required beans. • Database connections are configured. • Security settings are loaded. • Embedded Tomcat starts. • APIs become available to clients.

All of this happens automatically through Spring Boot's startup mechanism.

Interview Tip: A concise interview answer is:

"A Spring application starts when SpringApplication.run() is executed. Spring creates the ApplicationContext, scans components, creates and injects beans, applies auto-configuration, loads configuration properties, and starts the embedded server if it is a web application. Once initialization is complete, the application is ready to handle requests."