SpringBoot讀取配置檔案的6種方式
優先使用applilcation.properties中的資料,其次application.yml。
假如兩個實體Friend和Woman,分别讀取friend.properties和woman.properties檔案。如果woman.properties檔案中含有friend.properties檔案中的配置則會覆寫掉friend.properties檔案中的内容!因為先加載的friend類在加載的woman類,導緻配置檔案也先後讀取!
woman.properties
woman.name=女人
woman.age=20
friend.sex=女
friend.age=111
friend.properties
friend.sex=男
friend.age=222
@Value注解
@Component
public class Student {
@Value("${student.name}")
private String name;
@Value("${student.age}")
private Integer 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 +
'}';
}
}
@ConfigurationProperties注解
@ConfigurationProperties(prefix = "person")
@Component
public class Person {
private String lastName;
private Integer age;
private Boolean boss;
private Date birth;
private Map<String,Object> maps;
private List<Object> lists;
private Dog dog;
public String getLastName() {
return lastName;
}
public void setLastName(String lastName) {
this.lastName = lastName;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
public Boolean getBoss() {
return boss;
}
public void setBoss(Boolean boss) {
this.boss = boss;
}
public Date getBirth() {
return birth;
}
public void setBirth(Date birth) {
this.birth = birth;
}
public Map<String, Object> getMaps() {
return maps;
}
public void setMaps(Map<String, Object> maps) {
this.maps = maps;
}
public List<Object> getLists() {
return lists;
}
public void setLists(List<Object> lists) {
this.lists = lists;
}
public Dog getDog() {
return dog;
}
public void setDog(Dog dog) {
this.dog = dog;
}
@Override
public String toString() {
return "Person{" +
"lastName='" + lastName + '\'' +
", age=" + age +
", boss=" + boss +
", birth=" + birth +
", maps=" + maps +
", lists=" + lists +
", dog=" + dog +
'}';
}
}
@PropertySource配合@Value
@Component
@PropertySource(value = {"classpath:config/teacher.properties"})
public class Teacher {
@Value("${name}")
private String name;
@Value("${age}")
private Integer 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 "Teacher{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
@ConfigurationProperties配合@PropertySource配合@Value
@Component
@ConfigurationProperties(prefix = "woman")
@PropertySource(value = { "classpath:config/woman.properties" })
public class Woman {
@Value("${name}")
private String name;
@Value("${age}")
private Integer 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 "Woman{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
@ConfigurationProperties配合@Value
@Component
@ConfigurationProperties(prefix = "man")
public class Man {
@Value("${a}")
private String name;
@Value("${b}")
private Integer 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 "Man{" +
"name='" + name + '\'' +
", age=" + age +
'}';
}
}
Environment
@Test
public void testEnvironment() {
System.out.println(env.getProperty("student.name"));
}