SoFunction
Updated on 2025-04-25

Summary of five ways to read configuration files in Spring Boot

Spring Boot provides flexible and diverse ways to read configuration files (such as or ). This article introduces several common ways to read them.

1. Configuration file location and loading order

Spring Boot loads configuration files by default from the following locations (priority from high to low):

Command line parameters (such as: --=8081)

/ (located at classpath:/config/)

classpath:// or

External configuration centers (such as Nacos, Spring Cloud Config)

2. Summary of how to read configuration files

Spring Boot provides a variety of ways to read configuration files.

Method 1: Use the @Value annotation to read the configuration

This method is suitable for reading a single simple configuration item. It can be used to inject values ​​in the configuration file, or to specify default values ​​to prevent exceptions from being thrown when configuration items are missing.

Configuration file:

app:
  name: order-v
  version: v1

Sample code:

@Component
public class AppProperties {

    @Value("${:order}")
    private String name;

    @Value("${:v1.0.0}")
    private String version;

    public void print() {
        ("App Name: " + name);
        ("App Version: " + version);
    }
}
  • : The default value is followed
  • When there is no corresponding value in the configuration file, the default value will be used
  • If the configuration item has a corresponding value, the default value will not take effect

Test code:

@RestController
@RequestMapping("/test")
@AllArgsConstructor
public class TestController {

    private final AppProperties appProperties;

    @RequestMapping("/print")
    public void print() {
        ();
    }
}

✅ Pros:

Simple and straightforward, suitable for reading a single variable

❌ Disadvantages:

No nested structure, no batch binding, not conducive to maintenance

Method 2: Use @ConfigurationProperties to automatically bind the configuration class (recommended)

Suitable for binding multiple fields, nested structures, List, Map and other complex configurations.

Configuration file:

app:
  name: order-v
  version: v1
  servers:
    - http://dev-server:8080
    - http://test-server:8081
    - http://prod-server:8082
  metadata:
    author: Alice
    version: 1.0.2
    website: 
  modules:
    user:
      enabled: true
      path: /user
    admin:
      enabled: false
      path: /admin

Configuration class sample code:

import ;
import ;
import ;

import ;
import ;

@Component
@ConfigurationProperties(prefix = "app")
@Data
public class AppProperties {

    private String name;

    // List example    private List<String> servers;

    // Map<String, String> Example    private Map&lt;String, String&gt; metadata;

    // Map<String, Custom Object> Example    private Map&lt;String, Module&gt; modules;

    @Data
    public static class Module {
        private boolean enabled;
        private String path;
    }
    
    public void print() {
        ("name: " + name);
        ("servers: " + servers);
        ("metadata: " + metadata);
        ("modules: " + modules);
    }
}

Example of usage:

import ;
import ;
import ;
import ;
import ;


@RestController
@RequestMapping("/test")
@AllArgsConstructor
public class TestController {

    private final AppProperties appProperties;
    private final AppConfigProperties appConfigProperties;

    @RequestMapping("/print")
    public void print() {
        ();
    }

    @RequestMapping("/printConfig")
    public void printConfig() {
        ();
    }
}

✅ Pros:

  • Support object structure, nested objects, List, Map
  • Support verification (with @Validated)
  • Strong type binding, IDE support friendly

❌ Disadvantages:

Requires additional class definitions

Method 3: Use Environment to programmatically read the configuration

Suitable for dynamic reading and conditional judgment scenarios.

Sample code:

import ;
import ;
import ;
import ;
import ;
import ;
import ;

/***
 * @title
 * @author shijiangyong
 * @date 2025/4/24 17:28
 **/
@RestController
@RequestMapping("/test")
@AllArgsConstructor
public class TestController {

    private final AppProperties appProperties;
    private final AppConfigProperties appConfigProperties;
    private final Environment env;

    @RequestMapping("/print")
    public void print() {
        ();
    }

    @RequestMapping("/printConfig")
    public void printConfig() {
        ();
    }

    @RequestMapping("/printEnv")
    public void printEnv() {
        String name = ("");
        (" = " + name);
    }

}

✅ Pros:

  • Dynamic, flexible, supporting conditional judgment
  • Can be used in third-party libraries or tool classes

❌ Disadvantages:

Poor readability, automatic binding is not supported

Method 4: Load custom configuration file (@PropertySource)

Used when you need to read non-configuration files.

Custom configuration file:

=aliyun
=TPL001

Sample code:

import ;
import ;
import ;
import ;

/***
 * @title
 * @author shijiangyong
 * @date 2025/4/25 10:08
 **/
@Configuration
@PropertySource("classpath:")
@ConfigurationProperties(prefix = "sms")
@Data
public class SmsConfig {
    private String sign;
    private Template template;

    @Data
    public static class Template {
        private String code;
    }

    public void print() {
        ("sign : " + sign);
        (" : " + ());
    }
}

Example of usage:

import ;
import ;
import ;
import ;
import ;
import ;
import ;

/***
 * @title
 * @author shijiangyong
 * @date 2025/4/24 17:28
 **/
@RestController
@RequestMapping("/test")
@AllArgsConstructor
public class TestController {

    private final AppProperties appProperties;
    private final AppConfigProperties appConfigProperties;
    private final SmsConfig smsConfig;
    private final Environment env;

    @RequestMapping("/print")
    public void print() {
        ();
    }

    @RequestMapping("/printConfig")
    public void printConfig() {
        ();
    }

    @RequestMapping("/printEnv")
    public void printEnv() {
        String name = ("");
        (" = " + name);
    }


    @RequestMapping("/smsConfig")
    public void smsConfig() {
        ();
    }

}

✅ Pros:

  • Support loading of custom configuration files
  • Can be used with @ConfigurationProperties

❌ Disadvantages:

  • .yml format is not supported
  • Dynamic refresh is not supported

Method 5: Multi-environment configuration (multiple profile)

Suitable for configuration isolation for development, testing and production environments.

Configuration file:

# 
app:
  name: DevApp

# 
app:
  name: ProdApp

Activation method:

Set in:

spring:
  profiles:
    active: dev

Or through startup parameters:

--=prod

✅ Pros:

  • Multi-environment isolation, clear configuration
  • Support combination activation of multiple profiles

Summarize

Scene Recommended method
Read a single simple value @Value
Read nested structures and objects @ConfigurationProperties
Dynamic reading Environment
Non-default configuration file @PropertySource
Multi-environment support Multiple profiles

The above is the detailed content of the five ways to read configuration files in Spring Boot. For more information about Spring Boot reading configuration files, please follow my other related articles!