一、配置文件占位符
1、配置文件
person.lastName=李四${random.uuid}
person.age=${random.int}
person.birth=2017/12/15
person.boss=true
person.maps.key1=value1
person.maps.key2=value2
person.lists=a,b,c
person.dog.name=${person.lastName}_dog
person.dog.age=2
PS:${random.uuid}、${random.int}都是取得random的一些方法,也可以取我们上面自定义的配置属性,比如:${person.lastName}
2、JavaBean
@PropertySource(value = {"classpath:person.properties"})
@ConfigurationProperties(prefix = "person")
@Component
public class Person {
private String lastName;
private Integer age;
private boolean isBoss;
private Date birth;
private Map<String, Object> maps;
private List<Object> lists;
private Dog dog;
...省略getter/setter/toString()...
}
3、测试类
@RunWith(SpringRunner.class)
@SpringBootTest
public class Springboot02ConfigApplicationTests {
@Autowired
private Person person;
@Test
public void contextLoads() {
System.out.println(person);
}
}
4、输出结果
Person{lastName='李四44a6f753-30cd-4f12-a8e3-aaa42106a06f', age=-1912840149, isBoss=true, birth=Fri Dec 15 00:00:00 CST 2017, maps={key2=value2, key1=value1}, lists=[a, b, c], dog=Dog{name='李四a7c81e9a-ff3d-4c00-8ec6-f784e1ccad73_dog', age=2}}
二、广告
QQ群【Java初学者学习交流群】:458430385
微信公众号【Java码农社区】
- 今日头条号:编程界的小学生