SpringBoot_通过yml文件为变量赋值

0.1实体类

@Component//标记为springboot组件
@ConfigurationProperties(prefix = "people")//绑定yml配置文件 注意参数的首写字母小写
public class People {
    String name;
    int age;
    Date birth;
    Map maps;
    List lists;
    Cat cat;
} 
  

02.yml配置文件

注:yml不区分大小写,但可以使用松散命名,如nameName在yml中可写作name-name(虽然直接写成namename也行)

server:
  port: 8080

people:
  name: alan
  age: 3
  birth: 2019/09/01
  maps: {k1: v1,k2: v2,k3: v3}
  lists:
    - code
    - girl
    - game
  cat:
    name: Kayn
    age: 3

03.测试类

@SpringBootTest(classes = DemoApplication.class)//标记为springboot测试类 并指明Application类
public class anythingTest {
    @Autowired//注入yml配置文件中的数据
    private People people;

    @Test
    public void anything(){
        System.out.println(people);
    }
}

运行效果

你可能感兴趣的:(SpringBoot,spring,boot)