spring boot配置 properties与yaml配置各种类型

pom.xml

   <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-configuration-processor</artifactId>
            <optional>true</optional>
        </dependency>
java

public class Pet {

    private String type;
    private String name;

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getName() {
        return name;
    }

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

    @Override
    public String toString() {
        return new StringJoiner(", ", Pet.class.getSimpleName() + "[", "]")
                .add("type='" + type + "'")
                .add("name='" + name + "'")
                .toString();
    }
}

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

    private int id;
    private String name;

    private List hobby;

    private String family;

    private Map map;

    private Pet pet;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

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

    public List getHobby() {
        return hobby;
    }

    public void setHobby(List hobby) {
        this.hobby = hobby;
    }

    public String getFamily() {
        return family;
    }

    public void setFamily(String family) {
        this.family = family;
    }

    public Map getMap() {
        return map;
    }

    public void setMap(Map map) {
        this.map = map;
    }

    public Pet getPet() {
        return pet;
    }

    public void setPet(Pet pet) {
        this.pet = pet;
    }

    @Override
    public String toString() {
        return new StringJoiner(", ", Person.class.getSimpleName() + "[", "]")
                .add("id=" + id)
                .add("name='" + name + "'")
                .add("hobby=" + hobby)
                .add("family='" + family + "'")
                .add("map=" + map)
                .add("pet=" + pet)
                .toString();
    }
}


配置文件properties
person.id=1
person.family=我家
person.hobby=吃饭,睡觉,打豆豆
person.map.k1=张三
person.map.k2=李四
person.pet.type=狗
person.pet.name=旺财

yaml

person:
  family: 我家
  name: 张三
  id: 3
  hobby:
    吃饭,
    睡觉,
    打平顺
  map:
    k1: v1
    k2: v2
  pet:
    name: 看法
    type:server:
  port: 8081
  servlet:
    context-path: /

在这里插入图片描述

yaml list

在这里插入图片描述

ymal -map

在这里插入图片描述

person:
  family: 我家
  name: 张三
  id: 3
  hobby: [吃饭,睡觉,打平顺]
  map: {k1: v1,k2: v2}
  pet: {name: 看法,type:}
server:
  port: 8081
  servlet:
    context-path: /

测试

@RunWith(SpringRunner.class)
@SpringBootTest(classes = Task01Appliction.class)
public class DemoTest01 {

    @Autowired
    private DemoController demoController;

    @Autowired
    Person person;

    @Test
    public void testDemo01(){

        System.err.println(person);
    }
}

Person[id=1, name=‘null’, hobby=[吃饭, 睡觉, 打豆豆], family=‘我家’, map={k1=张三, k2=李四}, pet=Pet[type=‘狗’, name=‘旺财’]]

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值