SoFunction
Updated on 2025-05-05

Use of properties and yml in SpringBoot configuration file

1. The role of configuration files

The configuration file is mainly to solve the problems caused by hard coding, putting the information that may change in a centralized place. When we start a program, the application reads data from the configuration file and loads it to run.

Using configuration files, programs can be enabled to complete the interaction between users and applications, or the interaction between applications and other applications.

SpringBoot supports and defines the format of configuration files, and at another level it also achieves the purpose of standardizing the integration of other frameworks into SpringBoot. Many project or framework configuration information is also placed in configuration files, such as:

  • The project's startup port;
  • Database connection information (including settings of username and password);
  • Information such as calling keys of third-party systems;
  • Ordinary logs and exception logs for discovering and locating problems, etc.

2. Configuration file format

There are three types of Spring Boot configuration files:

yml is the abbreviation of yaml, and it appears most frequently in actual development. yaml and yml are used the same way.

NOTE:

  • In theory, .properties and .yml can exist in a project, and when .properties and .yml coexist, both configurations will be loaded. If the configuration file content conflicts, .properties are the main ones, that is, .properties have higher priority;
  • Although theoretically speaking, .properties can coexist with .yml, in actual business, a unified configuration file format is usually adopted, which can be maintained (reduced failure rate).

3. Properties configuration file description

The properties configuration file is the earliest configuration file format and is also the default configuration file for creating SpringBoot projects.

3.1 properties basic syntax

properties are configured as key values, and key and value are connected with "=", such as:

# Configure project port number=8080
#Configure database connection information=jdbc:mysql://127.0.0.1:3306/testdb?
characterEncoding=utf8&useSSL=false
=root
=root

3.2 Reading configuration files

If you want to actively read the contents in the configuration file in the project, you can use the @Value annotation to achieve this. The @Value annotation is read in the format of "${}", as shown in the following code:

The properties are configured as follows:

mykey.key1 = bite

import ;
import ;
import ;

@RestController
public class PropertiesController {
    @Value("${mykey.key1}")
    private String key1;

    @RequestMapping("/key")
    public String key(){
      return "Read value:"+key1;
 }
}

4. YML configuration file description

yml is YAML, its full name is Yet Another Markup Language, and its translation is "another markup language".

4.1 Basic yml syntax

yml is a configuration file of a tree structure, and its basic syntax is "key: value". The key and value are composed of English colon plus spaces.Spaces cannot be omitted

The basic grammar is as follows:

spring:
 datasource:
  url: jdbc:mysql://127.0.0.0:3306/dbname?characterEncoding=utf8&useSSL=false
  username: root
  password: root

4.2 yml reads the file

The way yml reads configuration is the same as properties, just use the @Value annotation, and the implementation code is as follows:

yml configuration:

string:
 hello: bite
@RestController
public class ReadYml {
  @Value("${}")
  private String hello;

  @RequestMapping("/ymlKey")
  public String key(){
     return "Read value:"+hello;
  }
}

NOTE: value plus odd and double quotes

  • By default, strings do not need to be added single or double quotes. If English single and double quotes are added, they can represent special meanings.
  • Single quotes will escape special characters, causing them to lose their special functions, and are always an ordinary string; such as \n
  • Double quotes will not escape special characters in strings, special characters will represent their own meaning.

yml configures different data types and null:

#The following format does not represent the yml file format, but is only used as an explanation
# string: Hello
  
# boolean value, true or false: true
boolean.value1: false

# integer: 10

# Floating point number: 3.14159

# Null, ~ stands for null: ~

# "" Empty string# Just add nothing directly afterwards, but this method is not intuitive, and more of the representation is to use quotes: ''

4.3 yml usage advanced

4.3.1 Configuration Objects

Configure objects in the yml file as follows:

student:
 id: 1
 name: Java
 age: 18


#Or can use in-line writingstudent: {id: 1,name: Java,age: 18}

At this time, @Value cannot be used to read objects in the configuration. At this time, another annotation @ConfigurationProperties should be used to read. The specific implementation is as follows:

import ;
import ;
import ;
@ConfigurationProperties(prefix = "student")
@Component
@Data
public class Student {
 private int id;
 private String name;
 private int age;
}

The calling class is implemented as follows:

@RestController
public class StudentController {

 @Autowired
 private Student student;
 
 @RequestMapping("/readStudent")
 public String readStudent(){
     return ();
 }
}

4.3.2 Configuration collection

The configuration file can also configure the list collection as follows:

dbtypes:
 name:
  - mysql
  - sqlserver
  - db2

The reading of a collection is like an object, and it is also read using @ConfigurationProperties. The specific implementation is as follows:

@Component
@ConfigurationProperties("dbtypes")
@Data
public class ListConfig {
  private List<String> name;
}

The implementation of accessing a collection is as follows:

@RestController
public class ReadYml2 {

 @Autowired
 private ListConfig listConfig;

 @RequestMapping("/readList")
 public String readList(){
    return ();
 }
}

4.3.3 Configuring Map

The configuration file can also be configured with a map, as shown below:

maptypes:
 map:
  k1: kk1
  k2: kk2
  k3: kk3

Map reads like objects, and is also read using @ConfigurationProperties. The specific implementation is as follows:

@Component
@ConfigurationProperties("maptypes")
@Data
public class MapConfig {
 private HashMap<String,String> map;
}

The printing class is implemented as follows:

@RestController
public class ReadYml2 {

 @Autowired
 private MapConfig mapConfig;

 @RequestMapping("/readMap")
 public String readStudent(){
   return ();
 }
}

Summarize

The above is personal experience. I hope you can give you a reference and I hope you can support me more.