代码:
GrilApplication.java
@SpringBootApplication
public class GrilApplication { public static void main(String[] args) {
SpringApplication.run(GrilApplication.class, args);
}
}
application.yml
server:
port:
gril:
cupSize: B
age:
GrilProperties.java
@Component
@ConfigurationProperties(prefix = "gril")
public class GrilProperties {
private String cupSize;
private Integer age; public String getCupSize() {
return cupSize;
} public void setCupSize(String cupSize) {
this.cupSize = cupSize;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}
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