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(${})

 

 















 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM