继续浏览精彩内容
慕课网APP
程序员的梦工厂
打开
继续
感谢您的支持,我会继续努力的
赞赏金额会直接到老师账户
将二维码发送给自己后长按识别
微信支付
支付宝支付

005SpringBoot之配置文件占位符

BIG阳
关注TA
已关注
手记 474
粉丝 73
获赞 458

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

打开App,阅读手记
0人推荐
发表评论
随时随地看视频慕课网APP