博客
关于我
SpringBoot | 读取yaml配置文件并注入JavaBean
阅读量:373 次
发布时间:2019-03-04

本文共 1508 字,大约阅读时间需要 5 分钟。

???Spring Boot?????YAML??????????

?Spring Boot???????????????????????????????????????????????????????YAML???????????

1. ???????

???????????pom.xml?????Spring Boot?????????????????????????????????????

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

2. ?Person?????

????????????Person??????????????????????????????@Component?@ConfigurationProperties???

@Component@ConfigurationProperties(prefix = "person")public class Person {    public String name;    public int age;    public Date birthday;    public Map
map; public List
list;}
  • @Component????????????Spring?IOC????
  • @ConfigurationProperties(prefix = "person")?????????????????????person??????????

3. ????

??????????????Person??????@Autowired????????????????????????

import org.springframework.beans.factory.annotation.Autowired;import org.springframework.boot.test.context.SpringBootTest;@SpringBootTestpublic class DemoApplicationTests {    @Autowired    private Person person;    @Test    void contextLoads() {        System.out.println("?????" + person);    }}

4. ??????

????????????????????????????????????????

Person{name='zhangsan', age=20, birthday=Sun Nov 01 00:00:00 CST 2020, map={k1=v1, k2=v2}, list=[aaa, bbb, ccc]}

5. ??????

?resources????????applications.yml????????

person:  name: zhangsan  age: 20  birthday: 2020/11/1  map:    k1: v1    k2: v2  list:    - aaa    - bbb    - ccc

??????????????????????????????Person????????????????????????????????????????????

转载地址:http://yayg.baihongyu.com/

你可能感兴趣的文章
Objective-C实现加密哈希SHA-1 算法(附完整源码)
查看>>
Objective-C实现动态规划之棒材切割算法(附完整源码)
查看>>
Objective-C实现勒让德多项式(附完整源码)
查看>>
Objective-C实现匹配字符串(附完整源码)
查看>>
Objective-C实现区域生长法(附完整源码)
查看>>
Objective-C实现区域生长法(附完整源码)
查看>>
Objective-C实现十六进制转二进制算法(附完整源码)
查看>>