๐ What is Spring Boot, and why was it introduced?
๐ค Interview Scene: “Explain Spring Boot in 60 seconds.”
Me: “It’s the opinionated way to build Spring apps fast—no XML, embedded server, smart auto-config, production-ready Actuator. You write business logic; it handles the wiring.”
⚡ Spring Boot = Spring + opinionated defaults + starter dependencies + auto-configuration + embedded server + Actuator. It was created to crush the “Spring setup/configuration tax” and make going to production boringly easy.
๐ก Why Spring Boot was introduced
- Too much boilerplate: XML configs, servlet containers, and manual wiring slowed teams down.
- Dependency hell: Picking libraries + compatible versions was painful.
- Non-production by default: Metrics/health/logging were afterthoughts.
Spring Boot fixed this with:
- Starters (e.g.,
spring-boot-starter-web
): curated, compatible dependencies. - Auto-Configuration: creates beans based on your classpath & properties.
- Embedded Servers: Tomcat/Jetty/Undertow inside the JAR—no external app server.
- Actuator: health, metrics, info, env—production knobs out of the box.
๐ง Core Concept (in one line)
“Convention over configuration for Spring apps, with production in mind.”
๐ฌ How Spring Boot works (under the hood)
- @SpringBootApplication =
@Configuration
+@EnableAutoConfiguration
+@ComponentScan
. - Classpath scanning: detects what’s present (e.g., Spring MVC, Jackson, Data JPA).
- Conditional beans: Auto-config classes use
@ConditionalOnClass
,@ConditionalOnMissingBean
,@ConditionalOnProperty
to decide what to create. - Starters pull the right libraries; Boot applies sensible defaults.
- Embedded server starts automatically; your app is runnable via
java -jar
.
๐ผ️ Diagram: Auto-config flow
@SpringBootApplication | v SpringApplication.run() | v ComponentScan --> finds your @Component/@Service/@Controller | v @EnableAutoConfiguration --> loads auto-config classes | (WebMvcAutoConfiguration, JacksonAutoConfiguration, ...) v Conditions evaluated (classpath, properties, missing bean?) | +--> True --> define default beans (ObjectMapper, RestTemplate, DataSource, ...) | +--> False --> skip (you keep your own custom beans)
๐งฉ Minimal example (controller + run)
// DemoApplication.java
@SpringBootApplication
public class DemoApplication {
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
// HelloController.java
@RestController
@RequestMapping("/api")
public class HelloController {
@GetMapping("/hello")
public Map<String, String> hello() {
return Map.of("message", "Hello, Spring Boot!");
}
}
server:
port: 8081
management:
endpoints:
web:
exposure:
include: health,info
Run: mvn spring-boot:run
or java -jar target/app.jar
๐ Surprise facts (to impress interviewers)
- Auto-config is opt-out: Your custom bean wins over Boot’s default (
@ConditionalOnMissingBean
). - Disable selectively:
@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)
. - Application lifecycle hooks:
ApplicationRunner
/CommandLineRunner
run after the context is ready. - Actuator is extensible: Create custom health/metrics to expose domain-specific signals.
- Layered JARs: Boot can build layered jars for fast Docker rebuilds (
spring-boot:repackage
with layers).
๐ชค Common gotchas
- Wrong scanning root: If your main class isn’t at a top-level package, component scanning misses beans.
- Shadowing beans accidentally: Duplicated types without
@Primary
/@Qualifier
→ ambiguity errors. - Auto-config surprises: Extra libs on classpath (e.g., H2, Thymeleaf) can trigger auto-configs you didn’t expect.
๐ 60-second quiz
- What three annotations does
@SpringBootApplication
bundle? - What wins if both your bean and auto-config define the same type?
- How do you disable one auto-configuration class?
๐งต Wrapping Up
Spring Boot exists to reduce cognitive load and speed up delivery. It gives you production-grade features from day one and lets you override anything when you need control.
Your turn: Have you ever been surprised by an auto-configuration? Drop the story and the fix you used — others will learn from it! ๐
Hashtags: #SpringBoot #Java #Microservices #Actuator #AutoConfiguration
Comments
Post a Comment