SoFunction
Updated on 2025-03-08

Spring Boot How to configure yml configuration file to define collections, arrays and maps

1. @value Get configuration file

In the usual yml configuration files, we often use strings that configure basic data types, for example, the configuration log file is written as follows:

# Configure log output levellogging:
  # Specify the location of the logback configuration file  config: classpath:
  # The path to output from the file log  path: E:/logs/springboot_server
  # Log output level  level:
    root: info

To obtain the attribute value, you can use the @value annotation to achieve it, as follows:

@Value("${}")
private String path;    // Get the output path of the log file

2. Get configuration files in List collection

The first method

# Interceptor path interceptor or not intercept configurationinterceptorconfig:
  path:
    #Any request type is intercepted under this path    include:
      - /api/v1/token/api_token
      - /api/v1/yibaotong/save

The second method

# Interceptor path interceptor or not intercept configurationinterceptorconfig:
  path:
    #Any request type is intercepted under this path    include: [/api/v1/token/api_token,/api/v1/yibaotong/save]

Summarize

It should be noted here that defining a List collection cannot be used@valueAnnotation to get all values ​​of the List collection, you need to define a configuration class bean and then use@ConfigurationPropertiesAnnotation to get list collection value, as follows:

@Data
@Component
@ConfigurationProperties(prefix = "") // Prefix of configuration filepublic class InterceptorPathBean
{
    /*
      * Paths that need to be intercepted
      */
    private List<String> include;
}

3. Define the object list collection to obtain configuration files

Single object List

First create a user object as follows:

@Data
public class User implements Serializable
{  
    private static final long serialVersionUID = 1L;
    private String appId;
    private String password;
}

Then the yml configuration file is written as follows:

jwt:
  userlist:
    - appId: YiBaoTong
      password: 123456
    - appId: ZhiKe
      password: 123456

Define the configuration bean to use the @ConfigurationProperties annotation to get the object collection value:

@Data
@Component
@ConfigurationProperties(prefix = "jwt") // Prefix of configuration filepublic class JwtConfigBean
{
    /**
      * User list
      */
    private List<User> userlist;
}

List object contains List

Define the configuration bean to use the @ConfigurationProperties annotation to get the object collection value:

@Data
@Component
@ConfigurationProperties(prefix = "jwt") // Prefix of configuration filepublic class JwtConfigBean {
    /**
      * User list
      */
    private List<UserTest> userList;
    @Data
    private static class UserTest {
        private String appId;
        private List<String> passwordList;
    }
}

yml file configuration

jwt:
  userList:
    - appId: '121212'
      passwordList: 'Active time flow node-PromoTimeValidNode, Activity time flow node-PromoTimeValidNode2'
    - appId: 'werw3313'
      passwordList: 'Active time flow node-PromoTimeValidNode, Activity time flow node-PromoTimeValidNode2'

4. Array acquisition configuration file

The yaml format is as follows:

interceptorconfig:
  path:
    includes: /api/v1,/api/v2  #Be careful to separate it with commas

The array value can be obtained through the @value annotation, as follows:

@Value("${}")
private String[] includes;

You can also get it by creating a configuration class bean and using the @ConfigurationProperties annotation, as follows:

@Data
@Component
@ConfigurationProperties(prefix = "") // Prefix of configuration filepublic class InterceptorPathBean
{  
    private String[] includes;
}
@Data
@Component
@ConfigurationProperties(prefix = "") // Prefix of configuration filepublic class InterceptorPathBean
{  
    private String[] includes;
}

5. Define the Map Collection Configuration File

The yaml format is as follows:

interceptorconfig:
  path:
    maps: {name: Xiao Ming,age: 24}

Or write it as:

interceptorconfig:
  path:
    maps:
      name: Xiao Ming
      age: 24

By creating the configuration class bean, use the @ConfigurationProperties annotation to get the map value, as follows:

@Data
@Component
@ConfigurationProperties(prefix = "") // Prefix of configuration filepublic class InterceptorPathBean
{
    private Map<String , String> maps;
}

The above is how the Spring Boot yml configuration file defines the basic data type and references the data type;

6. Reference link

Spring boot's yml configuration file defines list collections, arrays, maps, and errors occur during use

This is the article about Spring Boot elegantly configuring yml configuration file definition collections, arrays and maps. For more related Spring Boot configuration yml configuration file content, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!