@ConfigurationProperties配置绑定
import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; @Component//组件加入容器中 @ConfigurationProperties(prefix = "person")//Person中的属性与配置文件中person前缀下所有属性一一对应 public class Person { private String name; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
@RestController public class Demo { @Autowired Person person; @GetMapping("/test") public String test(){ return person.getName(); } }