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; }
Invest now in Acorns!!! 🚀
Join Acorns and get your $5 bonus!
Acorns is a micro-investing app that automatically invests your "spare change" from daily purchases into diversified, expert-built portfolios of ETFs. It is designed for beginners, allowing you to start investing with as little as $5. The service automates saving and investing. Disclosure: I may receive a referral bonus.
Invest now!!! Get Free equity stock (US, UK only)!
Use Robinhood app to invest in stocks. It is safe and secure. Use the Referral link to claim your free stock when you sign up!.
The Robinhood app makes it easy to trade stocks, crypto and more.
Webull! Receive free stock by signing up using the link: Webull signup.
More Related questions...
