Spring Boot
What happens during Spring Boot startup?
Spring Boot startup involves creating the application context, scanning beans, processing auto-configuration, wiring dependencies, initializing the embedded server, and preparing the application to serve requests.
The Short Answer
Spring Boot startup is the process of building the application context, creating beans, wiring dependencies, applying auto-configuration, and starting the embedded server.
The Big Picture
From the application developer's perspective, startup may appear as simple as:
SpringApplication.run(App.class, args);But internally, Spring Boot performs a large amount of orchestration before the application becomes ready.
Step 1: Main Method Starts SpringApplication
@SpringBootApplication
public class App {
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
}This is the entry point. Spring Boot creates a SpringApplication object and begins the startup lifecycle.
One important thing happens immediately:
@SpringBootApplicationThis annotation combines:
@Configuration
@ComponentScan
@EnableAutoConfigurationThat single annotation is one reason Spring Boot feels so compact.
Step 2: Build the ApplicationContext
The ApplicationContext is the central container that manages Spring beans and dependency injection.
Think of ApplicationContext as:
the giant runtime object graph manager for your application
During startup, Spring Boot creates the ApplicationContext and begins discovering components.
Step 3: Component Scanning
Spring scans packages looking for annotations like:
@Component
@Service
@Repository
@Controller
@RestControllerThese classes become Spring-managed beans.
@Service
@Repository
@RestController
Spring registers metadata about these classes so it can later create and inject them.
Step 4: Dependency Injection
Spring now creates bean instances and wires dependencies together.
@Service
public class OrderService {
private final PaymentService paymentService;
public OrderService(PaymentService paymentService) {
this.paymentService = paymentService;
}
}Spring sees that OrderService depends on PaymentService and injects the dependency automatically.
Step 5: Auto-Configuration
This is where Spring Boot becomes different from traditional Spring.
Spring Boot looks at:
- dependencies on the classpath
- application.properties / YAML
- existing beans
- environment settings
Then it automatically configures common infrastructure.
Dependency Detected
Auto Configuration
This is why adding one starter dependency can suddenly enable large amounts of framework behavior.
Step 6: Bean Lifecycle Callbacks
Some beans perform initialization work during startup.
@PostConstruct
public void init() {
System.out.println("Bean initialized");
}Spring also supports startup hooks like:
CommandLineRunner
ApplicationRunnerThese run after the context is fully initialized.
Step 7: Embedded Server Startup
If this is a web application, Spring Boot starts the embedded server.
Spring Boot App
Embedded Server
Ready
One reason Spring Boot became so popular is that applications became self-contained:
java -jar app.jarNo external application server installation required.
Why Startup Can Become Slow
Large Spring applications sometimes have slow startup times because:
- many beans are created
- classpath scanning is expensive
- reflection is used heavily
- database connections initialize
- caches warm up
- large dependency graphs exist
The Interview-Friendly Explanation
Common Interview Follow-Ups
What does @SpringBootApplication do?
It combines @Configuration, @ComponentScan, and @EnableAutoConfiguration.
What is ApplicationContext?
It is Spring's central container that manages beans, dependency injection, lifecycle, configuration, and application infrastructure.
What is auto-configuration?
Spring Boot automatically configures infrastructure based on classpath dependencies, properties, environment, and existing beans.
Why can startup become slow?
Large applications may have many beans, heavy reflection usage, expensive scanning, database initialization, cache warmup, and large dependency graphs.
What starts first: Tomcat or the ApplicationContext?
The ApplicationContext is initialized first. After the application is sufficiently prepared, Spring Boot starts the embedded server.