socue.github.io

yml文件与properties文件的区别

yml是Springboot默认支持的文件类型,与properties最大的区别为在内容的书写上更加简约,无重复性代码。

application.properties

server.port = 80
server.http2.enabled = true

application.yml

server:
    port: 80
    http2:
        enabled: true

使用SpringBoot读取文件内容

application.yml

itmayiedu:
  httpUrl: http://www.itmayiedu.com
  name: 蚂蚁课堂
  studens:
    money: 18000
@RestController
public class IndexController {
    @Value("${itmayiedu.httpUrl}")
    private String httpUrl;

    @Value("${itmayiedu.name}")
    private String name;

    @Value("${itmayiedu.studens.money}")
    private String money;

    @RequestMapping("/devtoolsindex")
    public String index(){
        return httpUrl+"_"+name+"_"+money;
    }
}