@Value
使用@Value一个个字段给值
@Component public class Man { @Value("${person.lastName}") private String lastName; @Value("12") private Integer age; @Value("false") private Boolean boss;
从application.properties里取值:
application.properties:
person.lastName = lisi person.age = 1
运行看结果:
@RestController public class FirstController { @Autowired Man man; @RequestMapping("/hello") public String helloWorld() { return man.toString(); } }
也可以直接对一个字段直接绑定使用:
application.properties:
person.lastName = lisi
person.age = 1
java:
@RestController public class FirstController { @Value("${person.age}") private Integer age; @RequestMapping("/hello") public String helloWorld() { return "age"+age; } }
结果: