一、代码结构
application.properties文件里配置
personal.province=JiangSu
personal.city=NanJing
PersonalProperties.java内容
@Component public class PersonalProperties { @Value("${personal.province}") private String province; @Value("${personal.city}") private String city; public String getProvince() { return province; } public void setProvince(String province) { this.province = province; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } }
DemoController.java内容
@RestController public class DemoController { @Autowired PersonalProperties personalProperties; @RequestMapping("/demo") private String demo() { return personalProperties.getCity() + " , "+ personalProperties.getProvince(); } }
启动DemoApplicatiuon.java
网页打开http://localhost:8080/demo
页面显示:NanJing , JiangSu