spring boot: ConfigurationProperties

时间:2023-03-08 17:00:24

读取配置信息

1.5 之前

@Component
@ConfigurationProperties(prefix = "user", locations= {"classpath:user.properties"})
public class UserConfig {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private int age;
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}

1.5 之后

@Component
@PropertySource(value = { "classpath:user.properties" })
@ConfigurationProperties(prefix = "user")
public class UserConfig { private String username;
private Integer age;
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}