application.properties
${testDefault:defaultValue}:默认值--》defaultValue
#person
person.name=詹金斯${random.uuid}
person.age=${random.int}
person.birth=2019/01/01
person.stage=true
person.maps.name=jack
person.maps.age=16
person.lists=a,b,c,d,e,f
person.child.name=${person.name}--${testDefault:defaultValue}--testName
person.child.age=11
Person.java
package com.atguigu.springboot.pojo;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.PropertySource;
import org.springframework.stereotype.Component;
import java.util.Date;
import java.util.List;
import java.util.Map;
/**
* @Classname Person
* @date 2019/8/24 12:15
*/
@Component
@ConfigurationProperties(prefix = "person")
public class Person {
private String name;
private Integer age;
private Boolean stage;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Child child;
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 getStage() {
return stage;
}
public void setStage(Boolean stage) {
this.stage = stage;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Child getChild() {
return child;
}
public void setChild(Child child) {
this.child = child;
}
@Override
public String toString() {
return "Person{" +
"name='" + name + '\'' +
", age=" + age +
", stage=" + stage +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", child=" + child +
'}';
}
}
测试:
import com.atguigu.springboot.pojo.Person;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.ApplicationContext;
import org.springframework.test.context.junit4.SpringRunner;
import javax.annotation.Resource;
@RunWith(SpringRunner.class)
@SpringBootTest
public class SpringBoot01HelloworldApplicationTests {
@Autowired
private Person person;
@Test
public void contextLoads() {
System.out.println(person.toString());
}
}
结果:
Person{name='詹金斯cd917e06-c7a8-4a47-beb0-05eaa7c4df9b', age=-1936523024, stage=true, birth=Tue Jan 01 00:00:00 CST 2019, maps={age=16, name=jack}, lists=[a, b, c, d, e, f], child=Child{name='詹金斯0e902f4e-c659-4c01-94bc-5a75e6eaa5e4--defaultValue--testName', age=11}}