springboot basics--springboot configuration instructions

1. Configuration files in springboot

1. Why does springboot still need to use configuration files?

  1. It is convenient for us to modify the default configuration of springboot;
  2. We have other information that needs to be saved in the configuration file;

2. What are the configuration files in springboot?

  1. properties configuration file;
  2. yml configuration file;

3. Things to note when using configuration files in springboot

  1. The file is placed in the src/main/resources directory or the classpath/config directory;
  2. Springboot reads configuration files starting with application by default

2. Yml syntax description

1. yml syntax

Basic syntax of yml

  • key: the format of value (the space in front of the value cannot be less, there can be more than one, and it cannot be replaced by tab);
  • Case Sensitive;
  • Strings do not need to use quotes by default. The difference between single quotes and double quotes is whether escape characters can be used;
  • Comment method: #

Three data types supported by yml

  • Literal: literal, a single value that cannot be split (number, string, Boolean)
  • Object: exists in the form of key-value pairs
  • Array: a collection of literals/objects

3. yml demonstration

1. Create configuration file

src/main/resources/application.yml

person:
  name: zhangsan
  sex: famale
  age: 19
  isMerried: false
  books: [三国演义,红楼梦,水浒传]
  pets:
    - 金毛
    - 多不多啦
    - 吉娃娃
  friends:
    [{
    
    name: zhangxueyou,sex: male},{
    
    name: 刘德华,sex:}]


2. Define entity classes

src/main/java/com/study/springboot02/bean/Friend.java

import org.springframework.stereotype.Component;


@Component
public class Friend {
    
    
    private String name;
    private String sex;

    public String getName() {
    
    
        return name;
    }

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

    public String getSex() {
    
    
        return sex;
    }

    public void setSex(String sex) {
    
    
        this.sex = sex;
    }

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

src/main/java/com/study/springboot02/bean/Person.java


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;


@Component
@ConfigurationProperties(prefix = "person")
public class Person {
    
    

    private String name;
    private String sex;
    private Integer age;
    private boolean isMerried;
    private List<String> books;
    private String [] pets;
    private List<Friend> friends;

    public String getName() {
    
    
        return name;
    }

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

    public String getSex() {
    
    
        return sex;
    }

    public void setSex(String sex) {
    
    
        this.sex = sex;
    }

    public Integer getAge() {
    
    
        return age;
    }

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

    public boolean isMerried() {
    
    
        return isMerried;
    }

    public void setMerried(boolean merried) {
    
    
        isMerried = merried;
    }

    public List<String> getBooks() {
    
    
        return books;
    }

    public void setBooks(List<String> books) {
    
    
        this.books = books;
    }

    public String[] getPets() {
    
    
        return pets;
    }

    public void setPets(String[] pets) {
    
    
        this.pets = pets;
    }

    public List<Friend> getFriends() {
    
    
        return friends;
    }

    public void setFriends(List<Friend> friends) {
    
    
        this.friends = friends;
    }

    @Override
    public String toString() {
    
    
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", isMerried=" + isMerried +
                ", books=" + books +
                ", pets=" + Arrays.toString(pets) +
                ", friends=" + friends +
                '}';
    }
}


3. Test

src/test/java/com/study/springboot02/Springboot02ApplicationTests.java

@SpringBootTest
class Springboot02ApplicationTests {
    
    
    @Autowired
    private Person person;

    @Test
    void contextLoads() {
    
    
        System.out.println(person);
    }

}

4. Properties demonstration

src/main/java/com/study/springboot02/bean/Friend.java

import org.springframework.stereotype.Component;


@Component
public class Friend {
    
    
    private String name;
    private String sex;

    public String getName() {
    
    
        return name;
    }

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

    public String getSex() {
    
    
        return sex;
    }

    public void setSex(String sex) {
    
    
        this.sex = sex;
    }

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

src/main/java/com/study/springboot02/bean/Person.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;


@Component
@ConfigurationProperties(prefix = "person1")
public class Person {
    
    

    private String name;
    private String sex;
    private Integer age;
    private boolean isMerried;
    private List<String> books;
    private String [] pets;
    private List<Friend> friends;

    public String getName() {
    
    
        return name;
    }

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

    public String getSex() {
    
    
        return sex;
    }

    public void setSex(String sex) {
    
    
        this.sex = sex;
    }

    public Integer getAge() {
    
    
        return age;
    }

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

    public boolean isMerried() {
    
    
        return isMerried;
    }

    public void setMerried(boolean merried) {
    
    
        isMerried = merried;
    }

    public List<String> getBooks() {
    
    
        return books;
    }

    public void setBooks(List<String> books) {
    
    
        this.books = books;
    }

    public String[] getPets() {
    
    
        return pets;
    }

    public void setPets(String[] pets) {
    
    
        this.pets = pets;
    }

    public List<Friend> getFriends() {
    
    
        return friends;
    }

    public void setFriends(List<Friend> friends) {
    
    
        this.friends = friends;
    }

    @Override
    public String toString() {
    
    
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", isMerried=" + isMerried +
                ", books=" + books +
                ", pets=" + Arrays.toString(pets) +
                ", friends=" + friends +
                '}';
    }
}


2. Create configuration file

server.port=9999

person1.name=lisi
person1.sex=famale
person1.age=19
person1.isMerried=false
person1.books[0]=三国演义
person1.books[1]=红楼梦
person1.pets[0]=金子
person1.pets[1]=银子
person1.pets[2]=童子
person1.friends[0].name=zhangsan
person1.friends[0].sex=male
person1.friends[1].name=lisi
person1.friends[1].sex=female

3. Test

src/test/java/com/study/springboot02/Springboot02ApplicationTests.java

@SpringBootTest
class Springboot02ApplicationTests {
    
    
    @Autowired
    private Person person;

    @Test
    void contextLoads() {
    
    
        System.out.println(person);
    }

}

5. Precautions

When application.properties and application.yml both exist,

优先读取application.properties的配置内容

No configuration file is specified, default application.properties and application.yml configuration files

6. How to deal with garbled code problems

The encoding needs to be configured in Idea as utf-8

If garbled characters are still displayed after setting, you need to add a sentence to the properties:

spring.http.encoding.enabled=true

7. Value injection of yml

Insert image description here

src/main/java/com/study/springboot02/bean/Person.java

import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;


@Component
//@ConfigurationProperties(prefix = "person")
public class Person {
    
    
    @Value("${person.name}")
    private String name;
    private String sex;
    private Integer age;
    private boolean isMerried;
    private List<String> books;
    private String [] pets;
    private List<Friend> friends;

    public String getName() {
    
    
        return name;
    }

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

    public String getSex() {
    
    
        return sex;
    }

    public void setSex(String sex) {
    
    
        this.sex = sex;
    }

    public Integer getAge() {
    
    
        return age;
    }

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

    public boolean isMerried() {
    
    
        return isMerried;
    }

    public void setMerried(boolean merried) {
    
    
        isMerried = merried;
    }

    public List<String> getBooks() {
    
    
        return books;
    }

    public void setBooks(List<String> books) {
    
    
        this.books = books;
    }

    public String[] getPets() {
    
    
        return pets;
    }

    public void setPets(String[] pets) {
    
    
        this.pets = pets;
    }

    public List<Friend> getFriends() {
    
    
        return friends;
    }

    public void setFriends(List<Friend> friends) {
    
    
        this.friends = friends;
    }

    @Override
    public String toString() {
    
    
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", isMerried=" + isMerried +
                ", books=" + books +
                ", pets=" + Arrays.toString(pets) +
                ", friends=" + friends +
                '}';
    }
}


src/test/java/com/study/springboot02/Springboot02ApplicationTests.java

@SpringBootTest
class Springboot02ApplicationTests {
    
    
    @Autowired
    private Person person;

    @Test
    void contextLoads() {
    
    
        System.out.println(person);
    }

}

8. Reading of custom properties

1. Customize a properties file and make the key different from the previous properties;
2. Use the propertysource annotation to indicate the name of the properties file you want to read;
3. Read in the same way as before, paying attention to modifying the value of prefix;

src/main/resources/person2.properties

person.name=lisi2
person.sex=famale2
person.age=12
person.isMerried=false
person.books[0]=三国演义2

src/main/java/com/study/springboot02/bean/Person.java


import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;

import java.util.Arrays;
import java.util.List;

@Component

@ConfigurationProperties(prefix = "person2")
public class Person {
    
    
    private String name;
    private String sex;
    private Integer age;
    private boolean isMerried;
    private List<String> books;
    private String [] pets;
    private List<Friend> friends;

    public String getName() {
    
    
        return name;
    }

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

    public String getSex() {
    
    
        return sex;
    }

    public void setSex(String sex) {
    
    
        this.sex = sex;
    }

    public Integer getAge() {
    
    
        return age;
    }

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

    public boolean isMerried() {
    
    
        return isMerried;
    }

    public void setMerried(boolean merried) {
    
    
        isMerried = merried;
    }

    public List<String> getBooks() {
    
    
        return books;
    }

    public void setBooks(List<String> books) {
    
    
        this.books = books;
    }

    public String[] getPets() {
    
    
        return pets;
    }

    public void setPets(String[] pets) {
    
    
        this.pets = pets;
    }

    public List<Friend> getFriends() {
    
    
        return friends;
    }

    public void setFriends(List<Friend> friends) {
    
    
        this.friends = friends;
    }

    @Override
    public String toString() {
    
    
        return "Person{" +
                "name='" + name + '\'' +
                ", sex='" + sex + '\'' +
                ", age=" + age +
                ", isMerried=" + isMerried +
                ", books=" + books +
                ", pets=" + Arrays.toString(pets) +
                ", friends=" + friends +
                '}';
    }
}


src/test/java/com/study/springboot02/Springboot02ApplicationTests.java

@SpringBootTest
class Springboot02ApplicationTests {
    
    
    @Autowired
    private Person person;

    @Test
    void contextLoads() {
    
    
        System.out.println(person);
    }

}

Guess you like

Origin blog.csdn.net/weixin_39213232/article/details/132029629