SpringBoot configuration file related

Table of contents

The difference between properties and yml

 The use of properties

  The use of yml

 yml configuration object


The content of the SpringBoot configuration file is divided into two categories:

     1. Spring's own configuration, such as server.port (this thing lies in application.properties by itself)

     2. Customized configuration

The format of the configuration file is divided into two types

     1.properties format

     2.yml format

The difference between properties and yml

    1. Versatility

properties is the default configuration file of the SpringBoot project! It is very old and only supports Java, and yml is a new configuration format that supports many languages, such as: Java, C/C++, Ruby, Python, Perl, C#, PHP wait

    2. Simplicity

Look at the properties first


spring.datasource.url=
spring.datasource.username=
spring.datasource.password=

​

These three parameters spring.datasource are written three times, redundant!

Look at how simple this yml is!

spring:
  datasource:
    url: 
    username: 
    password: 

3. Priority

 properties have higher priority than yml

 The use of properties

1. Registration configuration

Write directly in this application.properties!

server.port=8080

mykey=keyy!!

Two, use 

Then create a class under the demo directory

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class Read {
    @Value("${mykey}")
    private String key;
    @PostConstruct
    public void postConstruct(){
        System.out.println("success:"+key);
    }
}

 @Component is the five major types of annotations. If you want to use @Value annotations, you must first use the five major types of annotations

@Value is to introduce a certain configuration. This format must remember that the parentheses must use "" to wrap ${}, and then the parentheses are the key names defined by themselves (if I want to refer to the port, I must use @Value(" ${server.port}") That is to say, the full name of the key must be carried) 

Then use a String to receive this content, the variable can be named whatever

  The use of yml

Create an application.yml file under the resources path (parallel with .properties)

(It must be called this name to be recognized! Because " convention is greater than configuration "!!)

Look at the example just now (recursive this time)

 We don't use . to classify but like we do multi-level directories of files

spring: Level 1 datasource: Level 2 url username password Level 3

Now we directly start using yml

1. Prepare to configure

We write in this .yml file

server:
  port: 8090

mystr:
  str1: str1
  str2: str2
  str3: str3

 Notice! Notice! Notice! This: add a space after it, otherwise the content behind it will not be highlighted (and will not be recognized)

Two, use

This is no different from the previous. properties

package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class Read {
    @Value("${mystr.str1}")
    private String str1;
    @PostConstruct
    public void postConstruct(){
        System.out.println("success:"+str1);
    }
}

 Both configurations support multiple types of returns (need to receive corresponding type variables), here we use yml as an example

server:
  port: 8090

int:
  value: 18
float:
  value: 1.1
boolean:
  value: true
package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class Read {
    @Value("${int.value1}")
    private int intvalue;
    @Value("${float.value2}")
    private float floatvalue;
    @Value("${boolean.value3}")
    private boolean booleanvalue;
    @PostConstruct
    public void postConstruct(){
        System.out.println("int:"+intvalue);
        System.out.println("float:"+floatvalue);
        System.out.println("boolean:"+booleanvalue);
    }
}

Another point is that the string does not add single and double quotes by default. If double quotes are added, the escape characters in the string will take effect.

Single quotes turn special characters back into normal characters

str.str1=helloworld!
str.str2="hello\nworld"!
package com.example.demo;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class Read {
    @Value("${str.str1}")
    private String str1;
    @Value("${str.str2}")
    private String str2;
    @PostConstruct
    public void postConstruct(){
        System.out.println("str1:"+str1);
        System.out.println("str2:"+str2);
    }
}

 yml configuration object

There are two ways to write

student1:
  name: 张三
  age: 18

student2: {name: 李四,age: 20}

The first is the original writing

The second is the inline writing method. In the inline writing method, note that each key-value is separated by a comma and: there must be a space after it.

object use

@Component
@ConfigurationProperties(prefix="student1")
public class Student {
    private String name;
    private int age;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public String toString() {
        return "Student{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}

Objects cannot be used with @Value to be annotated with @ConfigurationProperties, and the following parameters are key

Note that there must be a setter method before it can be written!! (getter, tostring are generated by me with one click....)

Or adding a @Data annotation at this position is equivalent to writing Setter and getter methods! 

yml configuration collection

The configuration is roughly the same as that of the class

list:
  name:
    张三
    李四
    王五
package com.example.demo;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
@ConfigurationProperties(prefix="list")
@Data
public class Student {
    private List<String> name;

    @Override
    public String toString() {
        return "Student{" +
                "name=" + name +
                '}';
    }
}

 The only caveat is that 

 These two must be the same. In fact, it is easy to understand after thinking about it. There can be multiple secondary levels under the list. The prefix above encloses a list.

So how do you determine which level 2 it is? Then you can only use the variable name to determine

package com.example.demo;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;

@Component
public class Read {
    @Autowired
    Student student;
    @PostConstruct
    public void postConstruct(){
        System.out.println(student);
    }
}

Guess you like

Origin blog.csdn.net/chara9885/article/details/130627929