Skip to content

Spring Boot Configuration Guide

Both files configure Spring Boot applications.

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/test
spring.datasource.username=root
spring.datasource.password=1234
server:
port: 8080
spring:
datasource:
url: jdbc:mysql://localhost:3306/test
username: root
password: 1234
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=9090
server:
port: 8080

Result: 9090.


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-service
spring.cloud.config.uri=http://localhost:8888
flowchart TD
    A[bootstrap.properties] --> B[Connect to Config Server]
    B --> C[Load External Configuration]
    C --> D[application.properties]
    D --> E[Application Context Starts]

Spring Boot supports multiple configuration sources.

  1. Command-line arguments
  2. Environment variables
  3. Profile-specific configuration (application-{profile}.properties/.yml) — overrides the non-profile file at the same location
  4. External configuration files (e.g. a /config directory alongside the JAR, or --spring.config.location) — override the packaged config
  5. application.properties / application.yml packaged inside the JAR
  6. 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:

Terminal window
java -jar app.jar --server.port=9090

This overrides:

server.port=8080

An environment variable works the same way (SERVER_PORT=9090), and an external file can be pointed to directly:

Terminal window
java -jar app.jar --spring.config.location=/config/application.properties

Spring Boot also automatically picks up configuration from a /config directory alongside the JAR, without needing this flag.

Internally:

ConfigurableEnvironment
->
PropertySources
->
PropertyResolver

@Value("${server.port}")
private int port;

Limitations:

  • Not type-safe
  • Difficult for large groups of properties
  • No built-in validation
  • Harder to test

Configuration:

app:
name: OrderService
timeout: 30

Java:

@ConfigurationProperties(prefix = "app")
@Component
public class AppConfig {
private String name;
private int timeout;
// getters and setters
}

Usage:

@Autowired
AppConfig config;

Validation:

@Validated
@ConfigurationProperties(prefix = "app")
public class AppConfig {
@NotNull
private String name;
}
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
@Component
public class ConfigService {
@Value("${message}")
private String message;
}

Refresh configuration:

POST /actuator/refresh

Flow:

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 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:

@EnableConfigServer

Example endpoint:

http://localhost:8888/order-service/default

Each service’s configuration lives in its own file in the Git repository:

user-service.yml
order-service.yml
payment-service.yml

Benefits:

  • Centralized configuration
  • Environment-specific settings
  • Version-controlled configuration
  • Dynamic refresh support

Common profiles:

  • dev
  • test
  • prod

Files:

application.properties
application-dev.properties
application-test.properties
application-prod.properties

Activate:

spring.profiles.active=dev

Spring loads:

application.properties
+
application-dev.properties

Example:

application-dev.properties
server.port=8081
application-prod.properties
server.port=80

Example:

spring.profiles.active=dev,qa

Loading order:

  1. application.properties
  2. application-dev.properties
  3. application-qa.properties

If the same property exists in multiple profiles, the last active profile wins.

Example:

dev -> server.port=8081
qa -> server.port=9090
Final:
server.port=9090
  • application.yml is cleaner for hierarchical configuration, while .properties uses flat key-value pairs.
  • bootstrap.properties is 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.
  • @ConfigurationProperties is preferred over @Value for grouped, validated, and type-safe configuration.
  • @RefreshScope and /actuator/refresh allow runtime configuration refresh in Spring Cloud.
  • Spring profiles provide environment-specific configuration, and later active profiles override earlier ones.