springboot基础--springboot配置说明

一、springboot中的配置文件

1、springboot为什么还需要用配置文件

  1. 方便我们修改springboot默认的配置;
  2. 我们有其他的信息需要保存在配置文件中;

2、springboot中的配置文件有哪些

  1. properties配置文件;
  2. yml配置文件;

3、springboot中的配置文件使用中注意事项

  1. 文件放入在src/main/resources目录或者是类路径/config目录下;
  2. springboot默认读取以application开头的配置文件

二、yml 语法说明

1、yml语法

yml的基本语法

  • key: value的格式(value前面的空格不能少,可以有多个,不能用tab替代);
  • 大小写敏感;
  • 字符串默认不需要使用引号,单引号和双引号的区别在于是否能用转义字符;
  • 注释方式: #

yml支持的三种数据类型

  • 字面量:直接量,单个不能被拆分的值(数字、字符串、布尔)
  • 对象:键值对形式存在
  • 数组:字面量/对象的集合

三、 yml 演示

1、创建配置文件

src/main/resources/application.yml

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


2、定义实体类

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、测试

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

@SpringBootTest
class Springboot02ApplicationTests {
    @Autowired
    private Person person;

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

}

四、properties演示

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、创建配置文件

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、测试

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

@SpringBootTest
class Springboot02ApplicationTests {
    @Autowired
    private Person person;

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

}

五、注意事项

application.properties和application.yml同时都有的情况下,

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

未制定配置文件,默认application.properties和application.yml配置文件

六、乱码问题如何处理

需要在Idea中配置编码为utf-8

如果设置完后,还是显示乱码,需要在properties中添加一句:

springboot基础--springboot配置说明_第1张图片

spring.http.encoding.enabled=true

七、yml的value注入

springboot基础--springboot配置说明_第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 = "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);
    }

}

八、自定义properties的读取

1、自定义一个properties文件,并且让key和之前的properties不一样;
2、使用propertysource注解 来标明你要读取的properties文件名;
3、跟之前一样的读取方式,注意修改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);
    }

}

你可能感兴趣的:(#,springboot基础,spring,boot,java,spring)