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"));
	}

 

 

转载于:https://my.oschina.net/imlim/blog/1859091

posted @ 2021-11-04 10:34  _Phoenix  阅读(2207)  评论(0编辑  收藏  举报