代码:
GrilApplication.java
1 @SpringBootApplication 2 public class GrilApplication { 3 4 public static void main(String[] args) { 5 SpringApplication.run(GrilApplication.class, args); 6 } 7 }
application.yml
1 server: 2 port: 8089 3 gril: 4 cupSize: B 5 age: 18
GrilProperties.java
1 @Component 2 @ConfigurationProperties(prefix = "gril") 3 public class GrilProperties { 4 private String cupSize; 5 private Integer age; 6 7 public String getCupSize() { 8 return cupSize; 9 } 10 11 public void setCupSize(String cupSize) { 12 this.cupSize = cupSize; 13 } 14 15 public Integer getAge() { 16 return age; 17 } 18 19 public void setAge(Integer age) { 20 this.age = age; 21 } 22 }
HelloController.java
@Controller public class HelloController { @Autowired private GrilProperties grilProperties; @RequestMapping(value = "/hello",method = RequestMethod.GET) public String say(){ return grilProperties.getCupSize(); } }
访问地址http://127.0.0.1:8089/hello