Spring Boot Basics
Auto-configuration, starters, properties, profiles, Spring Boot internals
Spring Boot eliminates the configuration overhead of traditional Spring by providing auto-configuration, opinionated defaults, and embedded servers. Understanding what Spring Boot does automatically — and how to override it — is essential for building and debugging production Spring applications.
Key Points
- @SpringBootApplication: meta-annotation combining @Configuration + @EnableAutoConfiguration + @ComponentScan
- Auto-configuration: Spring Boot reads META-INF/spring/org.springframework.boot.autoconfigure.AutoConfiguration.imports and conditionally creates beans based on what's on the classpath
- @ConditionalOnClass, @ConditionalOnMissingBean, @ConditionalOnProperty: the conditions that gate auto-configuration
- Starters: spring-boot-starter-web pulls in Spring MVC, Tomcat, Jackson, validation — one dependency, no version conflicts
- application.properties / application.yml: externalized configuration — hierarchical keys like spring.datasource.url
- Profiles: spring.profiles.active=prod — load application-prod.yml overrides; use @Profile("prod") on beans
- @ConfigurationProperties: bind a prefix of properties to a typed POJO — better than @Value for groups of settings
- Embedded Tomcat: Spring Boot packages the server inside the JAR — no WAR deployment to external Tomcat required
- Spring Boot Actuator (spring-boot-starter-actuator): /actuator/health, /actuator/metrics, /actuator/env — production-ready endpoints
Spring Boot: YAML config, @ConfigurationProperties with records, @ConditionalOnProperty, profile overrides
// application.yml — hierarchical config
spring:
datasource:
url: jdbc:postgresql://localhost:5432/mydb
username: ${DB_USER}
password: ${DB_PASS}
jpa:
open-in-view: false # avoid OSIV anti-pattern
properties:
hibernate.default_batch_fetch_size: 100
app:
feature-flags:
new-checkout: true
mail:
from: noreply@example.com
max-retries: 3
# application-prod.yml (profile override)
logging.level.root: WARN
---
// @ConfigurationProperties — type-safe config binding
@ConfigurationProperties(prefix = "app.mail")
@Validated
public record MailConfig(
@NotBlank String from,
@Min(1) @Max(10) int maxRetries
) {}
@SpringBootApplication
@EnableConfigurationProperties(MailConfig.class)
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
// Conditional bean — only create if property is set
@Bean
@ConditionalOnProperty(name = "app.cache.enabled", havingValue = "true")
public CacheManager redisCacheManager(RedisConnectionFactory factory) {
return RedisCacheManager.builder(factory).build();
}Real-World Example
spring.jpa.open-in-view=true (default) keeps the Hibernate Session open for the entire HTTP request — including rendering the view — enabling lazy loading but causing unnecessary DB connections to be held. Disable it in production (open-in-view: false) and load everything you need in the service layer. This one setting causes the most unexpected N+1 queries in Spring Boot apps.