Spring / Spring Boot 4 Basics Interview Questions
What is Spring Boot's application.properties / application.yml and how does configuration work?
Spring Boot externalises application configuration through application.properties or application.yml files, environment variables, system properties, and more. The configuration property source hierarchy determines which value wins when the same key appears in multiple places.
| Priority | Source |
|---|---|
| 1 (highest) | Command-line arguments (--server.port=8081) |
| 2 | SPRING_APPLICATION_JSON environment variable |
| 3 | OS environment variables |
| 4 | application-{profile}.properties/yml (active profile) |
| 5 | application.properties / application.yml |
| 6 (lowest) | @PropertySource annotations on @Configuration classes |
# application.yml (YAML format - preferred for complex config) server: port: 8080 servlet: context-path: /api spring: application: name: order-service datasource: url: jdbc:postgresql://localhost:5432/orders username: ${DB_USER} # reference env variable password: ${DB_PASSWORD} jpa: hibernate: ddl-auto: validate show-sql: false # Bind to a typed configuration class: # application.yml: app: order: max-items: 50 allowed-currencies: [USD, EUR, GBP] // @ConfigurationProperties class: @ConfigurationProperties(prefix = "app.order") @Validated // enables Bean Validation on the properties public record OrderProperties( @Min(1) @Max(1000) int maxItems, @NotEmpty List<String> allowedCurrencies ) {} // Register: @SpringBootApplication @EnableConfigurationProperties(OrderProperties.class) public class App { ... } // Inject: @Service @RequiredArgsConstructor public class OrderService { private final OrderProperties props; }
More Related questions...