使用注解@Value映射
application.yml配置如下
person: name: zhangsan age: 18
实体Bean代码如下
@Controller public class QuickStartController { @Value("${person.name}") private String name; @Value("${person.age}") private Integer age; @RequestMapping("/quick") @ResponseBody public String quick(){ return "springboot 访问成功! name="+name+",age="+age; } }
使用注解@ConfigurationProperties映射
application.yml配置如下:
person: name: zhangsan age: 18
实体Bean代码如下:
@Controller @ConfigurationProperties(prefix = "person") public class QuickStartController { private String name; private Integer age; @RequestMapping("/quick") @ResponseBody public String quick(){ return "springboot 访问成功! name="+name+",age="+age; } public void setName(String name) { this.name = name; } public void setAge(Integer age) { this.age = age; } }