Skip to main content

Spring Boot Overview

Spring Boot builds on the Spring Framework to provide an opinionated, production-ready application platform. It auto-configures beans from classpath detection, binds externalized configuration through a layered property source chain, ships production metrics and health checks via Actuator, and provides test slices for fast, focused testing. Understanding the internals — not just the annotations — is what makes you effective when things go wrong.

Key Concepts at a Glance

  • Auto-Configuration: Mechanism that reads AutoConfiguration.imports, evaluates @ConditionalOn* conditions, and wires default beans — only if you haven't already defined your own.
  • @SpringBootApplication: Composed annotation combining @EnableAutoConfiguration, @ComponentScan, and @SpringBootConfiguration.
  • @ConditionalOnClass: Activates a @Configuration only if a specific class is present on the classpath.
  • @ConditionalOnMissingBean: Back-off guard — skips auto-config if the user has already defined a bean of the same type.
  • Starter: A POM-only dependency bundle that brings compatible libraries onto the classpath, triggering auto-configuration conditions.
  • @ConfigurationProperties: Binds a property namespace (payment.*) to a typed POJO with IDE auto-complete and startup validation.
  • @Value: Single-property injection supporting ${placeholder} and SpEL; best for one-off values.
  • Spring Profiles: Named environments (dev, prod) that load extra property files and activate profile-specific beans.
  • Actuator: Module that exposes management endpoints (/actuator/health, /actuator/metrics) for monitoring and operations.
  • Micrometer: Metrics facade embedded in Actuator; ships to Prometheus, Datadog, CloudWatch, and others.
  • @SpringBootTest: Loads the full ApplicationContext; slow but necessary for true integration tests.
  • @WebMvcTest: Slice that loads only Spring MVC; fast controller tests via MockMvc; services must be @MockBean.
  • @DataJpaTest: Slice that loads JPA + H2; auto-rollback after each test; repository tests without the web layer.
  • @MockBean: Replaces a real Spring bean with a Mockito mock inside the Spring test context.

Quick-Reference Table

Annotation / APIPurposeKey Notes
@SpringBootApplicationEntry point; enables auto-config, component scan, configCombines 3 annotations
@EnableAutoConfigurationTriggers loading of AutoConfiguration.importsUsed internally by @SpringBootApplication
@ConditionalOnClassGate config on classpath presenceEvaluated before any beans are created
@ConditionalOnMissingBeanBack-off if user-defined bean existsMust specify type; order-sensitive
@ConditionalOnPropertyGate config on property valueSupports havingValue and matchIfMissing
@ConfigurationPropertiesBind property namespace to POJOAdd @Validated for startup validation
@Value("${key:default}")Inject single property or SpEL expressionFragile; prefer @ConfigurationProperties for groups
spring.profiles.activeActivate named profilesSet via env var SPRING_PROFILES_ACTIVE=prod
spring.autoconfigure.excludeDisable a specific auto-config classPreferred over exclude = code attribute
management.endpoints.web.exposure.includeExpose Actuator endpoints over HTTPDefault: only health
management.server.portRun Actuator on separate portFirewall this port from public in production
management.endpoint.health.show-detailsControl health detail visibilitywhen-authorized in production
@SpringBootTest(webEnvironment=RANDOM_PORT)Full integration test on real HTTP portUse TestRestTemplate for HTTP calls
@WebMvcTest(Controller.class)MVC slice; loads only the target controllerFastest HTTP-layer test
@DataJpaTestJPA + H2 slice; auto-rollbackUse @AutoConfigureTestDatabase(replace=NONE) for real DB
@MockBeanReplaces bean with Mockito mock in contextUnique mock sets create separate contexts
@SpyBeanWraps real bean with Mockito spyUse when real behavior needed + verification
@TestPropertySourceOverride properties for a specific test classFiner-grained than @SpringBootTest(properties=)

Learning Path

Suggested reading order for a returning Java developer:

  1. Auto-Configuration — start here; understanding @ConditionalOnClass and @ConditionalOnMissingBean demystifies 90% of Spring Boot.
  2. Application Properties — learn the property source priority order, profiles, and why @ConfigurationProperties beats @Value.
  3. Spring Boot Starters — understand what each common starter wires up and how to swap embedded servers.
  4. Actuator — add production visibility; write custom health indicators and export metrics with Micrometer.
  5. Spring Boot Testing — master @WebMvcTest, @DataJpaTest, and context caching to build a fast, reliable test suite.

Top 5 Interview Questions

Q1: How does Spring Boot auto-configuration work? A: At startup, AutoConfigurationImportSelector reads all candidate @AutoConfiguration classes from META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports. Each class is guarded by @ConditionalOn* annotations — if all conditions pass, the class runs and registers its beans. Conditions that fail are skipped silently. Run with debug=true to see the full conditions evaluation report.

Q2: What is the difference between @ConfigurationProperties and @Value? A: @ConfigurationProperties binds an entire property namespace to a typed POJO, giving you type safety, IDE auto-complete, and @Validated startup validation. @Value injects a single property value by string key, which is fragile and hard to refactor. Use @ConfigurationProperties for any related group of settings; reserve @Value for single values or SpEL expressions.

Q3: Why is only the health Actuator endpoint exposed by default? A: Security by default — most endpoints expose sensitive data (bean graphs, environment properties with secrets, thread dumps). Spring Boot starts with the minimum safe configuration. You opt in to each additional endpoint via management.endpoints.web.exposure.include. In production, run Actuator on a separate management port and secure everything except health and info behind an ADMIN role.

Q4: What is the difference between @WebMvcTest and @SpringBootTest? A: @WebMvcTest loads only the Spring MVC layer (controllers, filters, exception handlers) and is very fast. Services and repositories are not loaded — mock them with @MockBean. Use it for controller-layer tests. @SpringBootTest loads the full ApplicationContext with all auto-configurations. It is slower but necessary for integration tests where multiple layers must interact.

Q5: How does Spring Boot handle context caching in tests? A: Spring caches the ApplicationContext by its configuration fingerprint (annotations, property sources, active profiles, and @MockBean types). Tests sharing the same fingerprint reuse the cached context — startup runs once. Adding a unique @MockBean creates a new fingerprint and a new context. Managing context reuse is the primary lever for reducing test suite startup time in large projects.


All Notes in This Domain

NoteDescription
Auto-ConfigurationHow @ConditionalOn* conditions wire beans from classpath detection
Application PropertiesProperty sources, profiles, @ConfigurationProperties, and @Value
Spring Boot StartersDependency bundles that trigger auto-configuration; common starters explained
ActuatorBuilt-in management endpoints, health indicators, metrics, and Micrometer
Spring Boot TestingTest slices, @MockBean, @SpringBootTest, and Testcontainers