springBoot 加载 配置文件 yml list map

student:
  name: god
  age: 10
  happy: true
  birthday: 2099/09/09
  mapscores:
    math: 100
    eng: 100
    chinese: 100
  hobbylist:
    - swimming
    - bycicle
    - tennis
    - basketball
  friend:
    name: friendname

 

package com.hellosrc.springconf.bean;

import org.springframework.beans.factory.annotation.Value;
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: cui song lin
 * @Description:
 **/
@Component
@ConfigurationProperties(prefix = "student")
public class Student {

    String name;
    Integer age;
    Boolean happy;
    Date birthday;
    Map<String, Integer> mapscores;
    List<String> hobbylist;
    Friend friend;

    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 getHappy() {
        return happy;
    }

    public void setHappy(Boolean happy) {
        this.happy = happy;
    }

    public Date getBirthday() {
        return birthday;
    }

    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }

    public Map<String, Integer> getMapscores() {
        return mapscores;
    }

    public void setMapscores(Map<String, Integer> mapscores) {
        this.mapscores = mapscores;
    }

    public List<String> getHobbylist() {
        return hobbylist;
    }

    public void setHobbylist(List<String> hobbylist) {
        this.hobbylist = hobbylist;
    }

    public Friend getFriend() {
        return friend;
    }

    public void setFriend(Friend friend) {
        this.friend = friend;
    }

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

}
package com.hellosrc.springconf.bean;

import org.springframework.stereotype.Component;

/**
 * @Author: cui song lin
 * @Description:
 **/
@Component
public class Friend {
    String name;

    public String getName() {
        return name;
    }

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

 

 

 

package com.hellosrc.springconf;

import com.hellosrc.springconf.bean.Student;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

@SpringBootTest
class SpringconfApplicationTests {
    @Autowired
    Student student;

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

}

 

 

 

测试结果

Student{name='god', age=10, happy=true, birthday=Wed Sep 09 00:00:00 CST 2099, mapscores={math=100, eng=100, chinese=100}, hobbylist=[swimming, bycicle, tennis, basketball], friend=com.hellosrc.springconf.bean.Friend@5853495b}
 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值