springboot2.0 配置文件取值

在配置文件application.properties 中任意定义一个值

random.value 代表随机数:

my.number=${random.value}
在controller 中,通过
  @Value(value = "${my.number}")
  private String value;

可以获得值。

例子:

  @RequestMapping("getnum")
    public Map<String,Object> getnum(@RequestParam String name){
        Map<String,Object> map =new HashMap<String, Object>();
        map.put("name",name);
        map.put("value",value);
        return map;
    }


也可以用yaml文件,application.yaml

my:
 number: ${random.int}
 name: zhangsan
 address: http://www.${my.name}.com


猜你喜欢

转载自blog.csdn.net/m0_38044453/article/details/80409191