springboot 获取配置文件属性值
两种方式
@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(${})


浙公网安备 33010602011771号