标签:rop 生效 cat 绑定 name pojo his date framework
1、在resource下创建一个application.yaml文件
person:
name: zhangsan
age: 20
birth: 1998/02/01
list:
- l1
- l2
- l3
map: {k1: v1,k2: v2}
2、创建一个Person类。
@ConfigurationProperties(prefix = "person"):将yaml文件中前缀是person下的属性与Peron类中的属性绑定。
如果要使@ConfigurationProperties(prefix = "person")生效,需要将组件注册到容器中。所以使用@Component注册组件。
package com.killbug.helloworld.pojo; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.Date; import java.util.List; import java.util.Map; @Component @ConfigurationProperties(prefix = "person") public class Person { private String name; private int age; private Date birth; private List list; private Map map; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } public Date getBirth() { return birth; } public void setBirth(Date birth) { this.birth = birth; } public List getList() { return list; } public void setList(List list) { this.list = list; } public Map getMap() { return map; } public void setMap(Map map) { this.map = map; } @Override public String toString() { return "Person{" + "name=‘" + name + ‘\‘‘ + ", age=" + age + ", birth=" + birth + ", list=" + list + ", map=" + map + ‘}‘; } }
SpringBoot02--将application.yaml配置文件中的属性和组件中的属性进行绑定
标签:rop 生效 cat 绑定 name pojo his date framework
原文地址:https://www.cnblogs.com/gongxiao/p/13445304.html