方式一:@Value绑定
基本类型属性注入,直接在字段上添加@Value(" x x x . x x x " ) 即 可 . 注 意 这 里 用 的 是 {xxx.xxx}")即可.注意这里用的是 xxx.xxx")即可.注意这里用的是,而不是#.@Value注入的属性,一般其他属性没有关联关系.
@RestController
public class ConfigPropertiesController {
@Value("${simos.name}")
private String name;
@Value("${simos.age}")
private int age;
@Value("${simos.bodyWeight}")
private double bodyWeight;
@Value("${simos.girlFriend.beautiful}")
private Boolean beautiful;
@RequestMapping(value = "/config",method = RequestMethod.GET)
public String getConfigProperties(){
return "{name:"+name+",age:"+age+",beautiful:"+beautiful+",bodyWeight:"+bodyWeight+"}";
}
}
方式二:使用默认获取
默认地址resource/application.yml,从环境变量中获取。