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=‘旺财’]]