Spring Boot¶
Spring Boot simplifies Spring application development with auto-configuration, embedded servers, and opinionated defaults. It eliminates boilerplate XML configuration. Key features: starter dependencies, application.properties/application.yml, actuator for monitoring, and the @SpringBootApplication annotation (combines @Configuration, @EnableAutoConfiguration, @ComponentScan).
@SpringBootApplication¶
@SpringBootApplication combines three annotations: @Configuration (marks as config class), @EnableAutoConfiguration (auto-configures beans based on classpath), @ComponentScan (scans current package + sub-packages). The main() method calls SpringApplication.run() which bootstraps the embedded server, creates the application context, and starts the app.
Deep Dive: What It Replaces
Auto-Configuration¶
Spring Boot automatically configures beans based on what's on the classpath. It reads META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports (Boot 3), and each auto-config class uses @Conditional* annotations to create beans only if conditions are met. Example: adding spring-boot-starter-web auto-configures Tomcat, DispatcherServlet, and Jackson. Exclude with @SpringBootApplication(exclude = {...}).
Deep Dive: How It Works
@EnableAutoConfigurationtriggers configuration imports- Each auto-config class has
@Conditional*annotations - Beans are created only if conditions are met
Example: spring-boot-starter-web on classpath → Tomcat + DispatcherServlet + error handling + Jackson ObjectMapper
// Exclude specific auto-config
@SpringBootApplication(exclude = {DataSourceAutoConfiguration.class})
public class MyApplication { }
Debug auto-config: debug=true in properties → prints conditions report on startup.
Starter Dependencies¶
Starters are curated dependency bundles — add one starter and it pulls in everything needed. No version management required (managed by parent POM). Examples: spring-boot-starter-web (Tomcat + MVC + Jackson), spring-boot-starter-data-jpa (Hibernate + HikariCP), spring-boot-starter-test (JUnit 5 + Mockito + AssertJ).
Deep Dive: Common Starters
| Starter | What It Brings |
|---|---|
spring-boot-starter-web |
Tomcat, Spring MVC, Jackson |
spring-boot-starter-data-jpa |
Hibernate, Spring Data JPA, HikariCP |
spring-boot-starter-security |
Spring Security, auto-config |
spring-boot-starter-test |
JUnit 5, Mockito, AssertJ, MockMvc |
spring-boot-starter-actuator |
Health checks, metrics, monitoring |
spring-boot-starter-validation |
Hibernate Validator |
Configuration Properties¶
Externalize config via application.yml or application.properties. Use @ConfigurationProperties(prefix = "app") for type-safe binding to POJOs. Resolution order (highest priority first): command-line args → SPRING_APPLICATION_JSON → application-{profile}.yml → application.yml → @PropertySource → defaults. Use profiles (application-dev.yml) for environment-specific config.
Deep Dive: Type-Safe Config & Profiles
# application.yml
app:
name: MyApp
max-retry: 3
allowed-origins:
- http://localhost:3000
- https://myapp.com
@ConfigurationProperties(prefix = "app")
@Component
public class AppProperties {
private String name;
private int maxRetry;
private List<String> allowedOrigins;
// getters and setters
}
Profile-specific:
# application-dev.yml
spring.datasource.url: jdbc:h2:mem:testdb
# application-prod.yml
spring.datasource.url: jdbc:postgresql://prod-db:5432/app
spring.profiles.active=dev
Embedded Server¶
Spring Boot embeds a web server (Tomcat by default) — no WAR deployment needed. Run as a self-contained JAR. Switch to Jetty or Undertow by excluding spring-boot-starter-tomcat and adding the alternative. Customize via server.port, server.servlet.context-path, server.tomcat.max-threads.
Deep Dive: Switching Servers
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-jetty</artifactId>
</dependency>
Actuator¶
Spring Boot Actuator provides production-ready features: health checks (/actuator/health), metrics (/actuator/metrics), environment info, bean listings, request mappings. Expose endpoints via management.endpoints.web.exposure.include. Create custom health indicators by implementing HealthIndicator.
Deep Dive: Endpoints & Custom Health
| Endpoint | Description |
|---|---|
/actuator/health |
Application health status |
/actuator/info |
Application info |
/actuator/metrics |
JVM, HTTP, custom metrics |
/actuator/env |
Environment properties |
/actuator/beans |
All registered beans |
/actuator/mappings |
All request mappings |
Common Interview Questions¶
Common Interview Questions
- What is Spring Boot? How is it different from Spring?
- What does
@SpringBootApplicationdo? - How does auto-configuration work?
- How do you disable a specific auto-configuration?
- What are starter dependencies?
- How does Spring Boot externalize configuration?
- What is the property resolution order?
- How do you switch from Tomcat to Jetty?
- What is Spring Boot Actuator? Name some endpoints.
- How do you create a custom health indicator?
- How do you use profiles in Spring Boot?