兩種方式
@ConfigurationProperties(prefix="person") @PropertySource(value="classpath:person.yml")
@ConfigurationProperties(prefix="person")這個注解指定配置文件中的person對象屬性
@Component //@ConfigurationProperties(prefix="person") @PropertySource(value="classpath:person.yml") public class Person { private String name; private Integer age; public Person() { } public Person(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 "Person{" + "name='" + name + '\'' + ", age=" + age + '}'; } }
直接輸出對象,就會得到屬性的值(注意這個對象不能new)
@PropertySource(value="classpath:person.yml") 這個注解是為了區分多配置文件相同屬性名,是需要指定配置文件路徑+名
取值,需要@Value(${})