@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