Configuration file for Spring Boot development

1 file type

1 properties

Same as previous properties usage.

Such as common database connection configuration files: jdbc.properties

jdbc.driver=com.mysql.jdbc.Driver
jdbc.url=jdbc:mysql://localhost:3306/test?characterEncoding=utf8
jdbc.username=root
jdbc.password=root

read configuration file

    @Test
    public void test1() throws IOException {
    
    
        InputStream inputStream = this.getClass().getResourceAsStream("jdbc.properties");
        Properties properties = new Properties();
        properties.load(inputStream);
        properties.list(System.out);
        System.out.println("==============================================");
        String property = properties.getProperty("jdbc.url");
        System.out.println("property = " + property);
    }

2 yaml

1 Description

YAML is a recursive acronym for "YAML Ain't Markup LanguageM (YAML is not a markup language). When this language was developed, YAML actually meant: "Yet Another Markup Language" (still a markup language).

Great for data-centric profiles

2 Grammar

  • key: value; there is a space between kv
  • Case Sensitive
  • Use indentation to indicate hierarchical relationships
  • Tabs are not allowed for indentation, only spaces are allowed
  • The number of spaces indented is not important, as long as the elements of the same level are left-aligned
  • #Represents comments
  • The string does not need to be quoted. If you want to add it, "and" M means that the string content will be escaped/unescaped

3 data types

  • Literals: A single, indivisible value. date, boolean, string, number, nul
k: v
  • Object: A collection of key-value pairs. map, hash, set, object
# 行内写法: 
k: {
    
    kl:vl,k2:v2,k3:v3}
# 或者
k:
    kl: vl
    k2: v2
    k3: v3
  • Array: A group of values ​​arranged in order. array, list, queue
# 行内写法: 
k: {
    
    vl,v2,v3}
# 或者
k:
    - vl
    - v2
    - v3

4 cases

@Data
public class Person {
    
    

    private String userName;
    private Boolean boss;
    private Date birth;
    private Integer age;
    private Pet pet;
    private String[] interests;
    private List<String> animal;
    private Map<String,Object〉score;
    private Set<Double> salarys;
    private Map<StringJ List<Pet>> allPets;
}
    
@Data
public class Pet {
    
    
    private String name;
    private Double weight;
}

Corresponding configuration file:

# yaml表示以上对象
person:
    userName: zhangsan
    boss: false
    birth: 2019/12/12 20:12:33
    age: 18
    pet:
      name: tomcat
      weight: 23.4
    interests: [篮球,游泳]
    animal:
         - jerry
         - mario
    score:
        english:
            first: 30
            second: 40
            third: 50
        math: [131,140,148]
        Chinese: {
    
    first: 128,second: 136}
    salarys: [3999,4999.98,5999.99]
    allPets:
        sick:
            - {
    
    name: tom}
            - {
    
    name: jerry,weight: 47}
        health:[{
    
    name: mario,weight: 47}]

3 Configuration Tips

There is generally no prompt for custom class and configuration file binding. You can increase the prompt by adding dependencies.

<dependency>
    <groupId>org.springframework.boot</groupId >
    <artifactId>spring-boot-configuration-processors/artifactId>
    <optional>true</optional〉
</dependency>
        
 < build>
    <plugins>
    < plugin>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-maven-plugin</artifactId>
        <configuration>
        <excludes>
            <exclude>
                <groupId >org.springframework.boot</gnoupId>
                <artifactId >spring- boot-configuration-processors</artifactId>
            </exclude>
        </excludes>
        </configuration>
    </plugin>
    </plugins>
 </build>

Guess you like

Origin blog.csdn.net/ABestRookie/article/details/127353630