springboot的yml配置文件使用示例

以普通javabean为例进行说明:
1.Person.java:
package com.example.bean;

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

import java.util.Date;
import java.util.List;
import java.util.Map;

/**

  • @author jack
  • @create 2019-07-06 11:51
    */

/**

  • 将文件中配置的每一个属性的值映射到这个组件中,@ConfigurationProperties
  • prefix = “person”:组件中所有的属性进行映射

*/

@Component
@ConfigurationProperties(prefix = “person”)
public class Person {
private String name ;
private Integer age ;
private Boolean boss ;
private Date birth ;
private Map<String,Object> maps ;
private List list ;
private Dog dog ;

public Person() {
}

@Override
public String toString() {
    return "Person{" +
            "name='" + name + '\'' +
            ", age=" + age +
            ", boss=" + boss +
            ", birth=" + birth +
            ", maps=" + maps +
            ", list=" + list +
            ", dog=" + dog +
            '}';
}

public String getName() {
    return name;
}

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

public Integer getAge() {
    return age;
}

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

public Boolean getBoss() {
    return boss;
}

public void setBoss(Boolean boss) {
    this.boss = boss;
}

public Date getBirth() {
    return birth;
}

public void setBirth(Date birth) {
    this.birth = birth;
}

public Map<String, Object> getMaps() {
    return maps;
}

public void setMaps(Map<String, Object> maps) {
    this.maps = maps;
}

public List<Object> getList() {
    return list;
}

public void setList(List<Object> list) {
    this.list = list;
}

public Dog getDog() {
    return dog;
}

public void setDog(Dog dog) {
    this.dog = dog;
}

public Person(String name, Integer age, Boolean boss, Date birth, Map<String, Object> maps, List<Object> list, Dog dog) {
    this.name = name;
    this.age = age;
    this.boss = boss;
    this.birth = birth;
    this.maps = maps;
    this.list = list;
    this.dog = dog;
}

}
需要注意添加不带参的构造函数
2.在pom.xml文件中添加 依赖:

org.springframework.boot
spring-boot-configuration-processor
true


3.新建application.yml文件,并注入属性:
server:
port: 8081
person:
name: zhangsan
age: 18
boss: false
birth: 2019/02/08
maps: {k1: v1,k2: v2}
list:
- lisi
- zhaoliu
dog:
name: 小狗
age: 18
注意格式:1.组件和属性之间需要回车到下一行并且属性和组件之间不能同一列起,必须用空格区分,属性名和属性值得书写格式:属性名:(空格)属性值
map键值之间也相同书写格式,如果是数组或者list集合或者Set集合,那么必须以-开头,书写格式:-(空格)数组或者集合元素
springboot测试类:
package com.example;

import com.example.bean.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit4.SpringRunner;

/**

  • springboot单元测试,可以在测试期间很方便的类似编码一样自动注入
    */
    @RunWith(SpringRunner.class)
    @SpringBootTest
    public class SpringBootConfigApplicationTests {
    @Autowired
    Person person ;
    @Test
    public void contextLoads() {
    System.out.println(person);
    }

}

上一篇:Python | Python_条件判断


下一篇:Django 利用自带API进行用户验证