#application.properties server.port=8888
使用 @Value 来给成员变量赋值
#application.properties msg=Hello world @RestController public class HelloWorldController { @Value("${msg}") private String msg; @RequestMapping("/hello") public String showMsg() { return this.msg; } }
打印出“Hello world”
#application.properties hello=dxh msg=Hello world ${hello} @RestController public class HelloWorldController { @Value("${msg}") private String msg; @RequestMapping("/hello") public String showMsg() { return this.msg; } }
此时 最终打印结果为 “Hello world dxh”
语法:
#生成一个随机数 ${random.int} #限定范围 ${random.int[1024,9999]}
#application.properties hello=dxh num=${random.int} msg=Hello world ${hello} ${random.int}
返回“Hello world dxh 2092427377 ” ; 加粗数字为随机数,因为properties文件在容器启动时只会被载入一次,因此这时再刷新将不会更改。
用处:配置随机值,再程序中如果有一些运算需要一个随机值,那么可以使用该方式来生成,且只生成一次。
#application.properties server.port=${random.int[1024,9999]}
用处 :在springcloud的微服务中,我们是不需要记录ip与端口号的。那么我们也就不需要去维护服务的端口号。让他随机就可以了。
是SpringBoot中新增的一种配置文件格式。
特点 :具备天然的树状结构
①配置文件的扩展名有变化
②配置文件中的语法有变化
server: port: 8888 hello: msg: Helloworld msg2: Dxh
原文链接:https://www.cnblogs.com/net-safe/p/13529194.html