Spring Boot Configuration Guide
application.properties vs application.yml
Section titled “application.properties vs application.yml”Both files configure Spring Boot applications.
application.properties
Section titled “application.properties”server.port=8080spring.datasource.url=jdbc:mysql://localhost:3306/testspring.datasource.username=rootspring.datasource.password=1234application.yml
Section titled “application.yml”server: port: 8080
spring: datasource: url: jdbc:mysql://localhost:3306/test username: root password: 1234Comparison
Section titled “Comparison”| Feature | application.properties | application.yml |
|---|---|---|
| Format | Key-value | Hierarchical |
| Readability | Harder for nested configs | Cleaner for nested configs |
| Repetition | High | Low |
| Spring Boot Support | Yes | Yes |
| Extension | .properties |
.yml / .yaml |
YAML is generally preferred for larger, more complex configuration trees.
There’s no functional difference between the .yml and .yaml extensions — .yaml is the official extension, .yml a shorter widely-supported alternative, and Spring Boot supports both equally.
If the same property is defined in both application.properties and application.yml, application.properties wins:
server.port=9090server: port: 8080Result: 9090.
bootstrap.properties
Section titled “bootstrap.properties”bootstrap.properties is primarily used in Spring Cloud applications and is loaded before application.properties.
Typical uses:
- Spring Cloud Config Server
- HashiCorp Vault
- Consul
- Encrypted configuration
Example:
spring.application.name=order-servicespring.cloud.config.uri=http://localhost:8888Loading Order
Section titled “Loading Order”flowchart TD
A[bootstrap.properties] --> B[Connect to Config Server]
B --> C[Load External Configuration]
C --> D[application.properties]
D --> E[Application Context Starts]
Externalized Configuration
Section titled “Externalized Configuration”Spring Boot supports multiple configuration sources.
Priority (Highest to Lowest)
Section titled “Priority (Highest to Lowest)”- Command-line arguments
- Environment variables
- Profile-specific configuration (
application-{profile}.properties/.yml) — overrides the non-profile file at the same location - External configuration files (e.g. a
/configdirectory alongside the JAR, or--spring.config.location) — override the packaged config application.properties/application.ymlpackaged inside the JAR- Default properties
Note: profile-specific config always wins over the base file at the same location tier — it doesn’t override an external base file from a higher tier.
Example override:
java -jar app.jar --server.port=9090This overrides:
server.port=8080An environment variable works the same way (SERVER_PORT=9090), and an external file can be pointed to directly:
java -jar app.jar --spring.config.location=/config/application.propertiesSpring Boot also automatically picks up configuration from a /config directory alongside the JAR, without needing this flag.
Internally:
ConfigurableEnvironment ->PropertySources ->PropertyResolver@ConfigurationProperties vs @Value
Section titled “@ConfigurationProperties vs @Value”Using @Value
Section titled “Using @Value”@Value("${server.port}")private int port;Limitations:
- Not type-safe
- Difficult for large groups of properties
- No built-in validation
- Harder to test
Using @ConfigurationProperties
Section titled “Using @ConfigurationProperties”Configuration:
app: name: OrderService timeout: 30Java:
@ConfigurationProperties(prefix = "app")@Componentpublic class AppConfig {
private String name; private int timeout;
// getters and setters}Usage:
@AutowiredAppConfig config;Validation:
@Validated@ConfigurationProperties(prefix = "app")public class AppConfig {
@NotNull private String name;}Comparison
Section titled “Comparison”| Feature | @Value |
@ConfigurationProperties |
|---|---|---|
| Type-safe | No | Yes |
| Groups properties | No | Yes |
| Validation | No | Yes |
| Cleaner code | No | Yes |
| Suitable for large configs | No | Yes |
Reloading Configuration Without Restarting
Section titled “Reloading Configuration Without Restarting”Use Spring Cloud with @RefreshScope.
@RefreshScope@Componentpublic class ConfigService {
@Value("${message}") private String message;}Refresh configuration:
POST /actuator/refreshFlow:
sequenceDiagram
participant Git
participant ConfigServer
participant Service
Git->>ConfigServer: Configuration updated
Service->>Service: POST /actuator/refresh
Service->>ConfigServer: Fetch latest configuration
ConfigServer-->>Service: Updated properties
Service->>Service: Refresh @RefreshScope beans
Spring Cloud Config Server
Section titled “Spring Cloud Config Server”Spring Cloud Config Server centralizes configuration for multiple microservices.
Architecture:
flowchart TD
A[Microservice 1]
B[Microservice 2]
C[Microservice 3]
A --> D[Spring Cloud Config Server]
B --> D
C --> D
D --> E[Git Repository]
Enable the server:
@EnableConfigServerExample endpoint:
http://localhost:8888/order-service/defaultEach service’s configuration lives in its own file in the Git repository:
user-service.ymlorder-service.ymlpayment-service.ymlBenefits:
- Centralized configuration
- Environment-specific settings
- Version-controlled configuration
- Dynamic refresh support
Profile-Based Configuration
Section titled “Profile-Based Configuration”Common profiles:
- dev
- test
- prod
Files:
application.propertiesapplication-dev.propertiesapplication-test.propertiesapplication-prod.propertiesActivate:
spring.profiles.active=devSpring loads:
application.properties+application-dev.propertiesExample:
server.port=8081server.port=80Multiple Active Profiles
Section titled “Multiple Active Profiles”Example:
spring.profiles.active=dev,qaLoading order:
application.propertiesapplication-dev.propertiesapplication-qa.properties
If the same property exists in multiple profiles, the last active profile wins.
Example:
dev -> server.port=8081qa -> server.port=9090
Final:server.port=9090Key Takeaways
Section titled “Key Takeaways”application.ymlis cleaner for hierarchical configuration, while.propertiesuses flat key-value pairs.bootstrap.propertiesis loaded before the application context and is mainly used with Spring Cloud external configuration.- Spring Boot supports externalized configuration from multiple sources with a defined precedence.
@ConfigurationPropertiesis preferred over@Valuefor grouped, validated, and type-safe configuration.@RefreshScopeand/actuator/refreshallow runtime configuration refresh in Spring Cloud.- Spring profiles provide environment-specific configuration, and later active profiles override earlier ones.