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