Configuration with application.properties and application.yml in Spring Boot


Table of Contents

  1. Introduction to Spring Boot Configuration
  2. application.properties vs application.yml
  3. Default Locations and Hierarchy
  4. Common Configuration Examples
  5. Profiles and Environment-Specific Configs
  6. External Configuration Sources
  7. Accessing Configuration in Code
  8. Using @ConfigurationProperties
  9. Validating Configuration Properties
  10. Best Practices
  11. Conclusion

1. Introduction to Spring Boot Configuration

Spring Boot makes it easy to externalize configuration using .properties or .yml files. This allows you to separate environment-specific values (like port numbers, database URLs, logging levels) from your source code, promoting clean code and scalability.


2. application.properties vs application.yml

Both application.properties and application.yml serve the same purpose, with different syntax:

Example – application.properties:

propertiesCopyEditserver.port=8081
spring.datasource.url=jdbc:mysql://localhost:3306/mydb
spring.datasource.username=root
spring.datasource.password=password

Equivalent in application.yml:

yamlCopyEditserver:
  port: 8081

spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb
    username: root
    password: password

Use .properties if you prefer flat key-value pairs and .yml if you want hierarchical configuration with better readability.


3. Default Locations and Hierarchy

Spring Boot looks for these files in the following order:

  1. application.properties or application.yml in src/main/resources/
  2. External config via command-line args or environment variables
  3. Config files specified via --spring.config.location
  4. Config files in config/ subdirectory

Priority: Command-line > External file > Internal file


4. Common Configuration Examples

Server:

propertiesCopyEditserver.port=8080
server.servlet.context-path=/api

Logging:

propertiesCopyEditlogging.level.org.springframework=INFO
logging.level.com.myapp=DEBUG

Database:

propertiesCopyEditspring.datasource.url=jdbc:postgresql://localhost:5432/db
spring.datasource.username=postgres
spring.datasource.password=secret
spring.jpa.hibernate.ddl-auto=update

Thymeleaf (template engine):

propertiesCopyEditspring.thymeleaf.cache=false

5. Profiles and Environment-Specific Configs

Spring Boot allows defining multiple profiles like dev, test, prod.

Structure:

  • application.properties → Default config
  • application-dev.properties → Dev config
  • application-prod.yml → Production config

Activate a profile:

propertiesCopyEditspring.profiles.active=dev

This can also be done via command line:

bashCopyEditjava -jar app.jar --spring.profiles.active=prod

6. External Configuration Sources

Besides internal files, Spring Boot can read config from:

  • Command-line arguments
  • Environment variables
  • Config data from a .jar
  • OS-level SPRING_APPLICATION_JSON
  • External config servers (e.g., Spring Cloud Config)

This makes it highly flexible for deploying to containers or cloud environments.


7. Accessing Configuration in Code

Use @Value to inject a value:

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

Injecting list or map:

yamlCopyEditapp:
  servers:
    - dev.example.com
    - prod.example.com
javaCopyEdit@Value("${app.servers}")
private List<String> servers;

8. Using @ConfigurationProperties

For grouped config values:

yamlCopyEditapp:
  title: My App
  features:
    enableAuth: true
    enablePayment: false

Define POJO:

javaCopyEdit@Component
@ConfigurationProperties(prefix = "app")
public class AppConfig {
    private String title;
    private Features features;

    public static class Features {
        private boolean enableAuth;
        private boolean enablePayment;
    }
}

Enable with:

javaCopyEdit@EnableConfigurationProperties

or annotate the POJO with @ConfigurationProperties.


9. Validating Configuration Properties

Spring Boot supports bean validation with @Validated:

javaCopyEdit@Component
@ConfigurationProperties(prefix = "app")
@Validated
public class AppConfig {
    @NotBlank
    private String title;
}

This ensures invalid configs are caught at startup, avoiding runtime errors.


10. Best Practices

  • Use .yml for structured configs, .properties for simpler setups.
  • Always define environment-specific profiles for better manageability.
  • Secure sensitive data using environment variables or secrets managers.
  • Group related properties with @ConfigurationProperties instead of multiple @Value.
  • Use validation to enforce correct configurations.

11. Conclusion

application.properties and application.yml are fundamental to configuring Spring Boot applications. They offer a centralized and flexible way to manage all settings, and understanding their usage is crucial for any Spring developer. As your application scales, these files become the blueprint of environment-specific behavior.