@Value註解的方式取值spring
設定appliction.properties的配置信息springboot
xiaoming.sex=boy xiaoming.age=18 xiaoming.score=98
使用@Value取值app
@RestController public class PersonController { @Value("${xiaoming.sex}") private String sex; @Value("${xiaoming.age}") private Integer age; @Value("${xiaoming.score}") private Integer score; @RequestMapping("/xiaoming") public String get() { return String.format("小明==》性別:%s-----年齡:%s-----分數:%s",sex,age,score); } }
頁面展現ide
小明==》性別:boy-----年齡:18-----分數:98測試
使用@ConfigurationProperties賦值給實體類this
設定appliction.yml的配置信息code
person: name: xiaoming age: 18
@ConfigurationProperties賦值給實體類orm
@Component @ConfigurationProperties(prefix = "person") public class Person { private String name; private Integer age; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } }
請求信息對象
@Autowired private Person person; @RequestMapping("/person") public String getPerson() { return String.format("姓名:%s-----年齡:%s",person.getName(),person.getAge()); }
頁面展現get
姓名:xiaoming-----年齡:18
經過注入獲取Environment對象,而後再獲取定義在配置文件的屬性值
設定appliction.properties的配置信息
springboot.test=hello-springboot
獲取Environment對象,而後再獲取定義在配置文件的屬性值
private static final String hello = "springboot.test"; @Autowired private Environment environment; @RequestMapping("/enviro") public String getenv() { return String.format("測試Environment:" + environment.getProperty(hello)); }
頁面展現
測試Environment:hello-springboot