手记

005SpringBoot之配置文件占位符

1、配置文件

person.lastName=李四${random.uuid}person.age=${random.int}person.birth=2017/12/15
person.boss=trueperson.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")@Componentpublic 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)@SpringBootTestpublic 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}}


作者:编程界的小学生
链接:https://www.jianshu.com/p/7abd807ed2ae

0人推荐
随时随地看视频
慕课网APP