二、SpringBoot配置绑定的两种方式
演示文件
bean
public class Student { private String name; private Integer age; public Student(){} public Student(String name, Integer age) { this.name = name; this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } @Override public String toString() { return "Student{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
properties
方式一(@Component+@ConfigurationProperties)
将@Component和@ConfigurationProperties都标在Student类上,@ConfigurationProperties(prefix = “stu”)中的prefix = "stu"表示将从配置文件中前缀为stu的配置项开始找。
方式二(@ConfigurationProperties+@EnableConfigurationProperties)
@ConfigurationProperties标在Student上,@EnableConfigurationProperties标在配置类上,Student在配置类中造。
Student类
配置类