How Properties Work in Spring Boot | Generated by AI

Home PDF

Let’s explore how properties work in Spring Boot, including how to use YAML or .properties files, how to override them with command-line arguments like -D, the various ways to initialize properties, and how to test them effectively.


How Properties Work in Spring Boot

Spring Boot provides a flexible system for managing application configuration through properties, which are key-value pairs. These properties can be defined in multiple sources, and Spring Boot resolves them based on a specific order of precedence. This allows you to customize your application for different environments or deployment scenarios. Properties are loaded into the Spring Environment, making them accessible throughout your application.

The main sources of properties include:


Using YAML or Properties Files

Spring Boot supports two primary formats for configuration files, both typically placed in src/main/resources:

1. .properties Files

This is a simple, flat key-value format:

server.port=8080
spring.datasource.url=jdbc:mysql://localhost:3306/mydb

2. .yml or .yaml Files

This is a structured, hierarchical format using indentation:

server:
  port: 8080
spring:
  datasource:
    url: jdbc:mysql://localhost:3306/mydb

Key Points:


Using Command-Line Arguments to Override Properties

You can override properties defined in configuration files using command-line arguments in two ways:

1. Using -- for Spring Boot Properties

Pass properties directly when running the application:

java -jar myapp.jar --server.port=8081 --spring.datasource.url=jdbc:mysql://localhost:3306/testdb

These take precedence over configuration files.

2. Using -D for System Properties

Set system properties with -D, which Spring Boot also recognizes:

java -Dserver.port=8081 -Dspring.datasource.url=jdbc:mysql://localhost:3306/testdb -jar myapp.jar

System properties override configuration file values as well.


Different Ways to Initialize Properties

Spring Boot offers several methods to define or initialize properties beyond files and command-line arguments:

1. Environment Variables

Properties can be set via environment variables. For example:

2. Java Code

You can initialize properties programmatically:

3. Default Values

Provide fallback values if a property isn’t defined:


Property Precedence

Spring Boot resolves properties from multiple sources in this order (higher precedence overrides lower):

  1. Command-line arguments (--property=value)
  2. System properties (-Dproperty=value)
  3. Environment variables
  4. Configuration files (application.properties or application.yml)
  5. Default values in code

Example: If server.port=8080 is in application.properties but you run java -jar myapp.jar --server.port=8081, the port will be 8081.


How to Test Properties

Testing properties ensures your application behaves as expected. Here are common approaches:

1. Using @TestPropertySource

Override properties in unit tests:

@SpringBootTest
@TestPropertySource(properties = {"server.port=9090"})
public class MyTest {
    // Test code
}

2. Using application-test.properties

Place a test-specific file in src/test/resources:

server.port=9090

Spring Boot loads this automatically during tests.

3. Programmatic Overrides

Set properties in test setup:

@SpringBootTest
public class MyTest {
    @BeforeEach
    public void setup() {
        System.setProperty("server.port", "9090");
    }
}

4. Using @ActiveProfiles

Activate profile-specific properties:


Summary

This covers the essentials of working with properties in Spring Boot. Let me know if you’d like more details!


Back 2025.03.10 Donate