转载

Spring Boot框架中读取配置的几种方式

现在开发的主力语言已经从PHP转向Java,目前参与或负责公司的几个项目都是使用Spring Boot框架。用了差不多两三个月的Java,对比PHP就一个感觉:繁琐,Java比PHP繁琐的很!由于自己平时也在努力学习Java中,今天趁着有空闲来总结一下Spring Boot中读取配置的几种方式,加深一下自己的印象。

读取application文件

在application.yml或者properties文件中添加:

info.address=RPC
info.company=Spring
info.degree=high

1.@Value注解读取方式

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

@Component
public class InfoConfig {
    @Value("${info.address}")
    private String address;
    @Value("${info.company}")
    private String company;
    @Value("${info.degree}")
    private String degree;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }
}

2.@ConfigurationProperties注解读取方式

@Component
@ConfigurationProperties(prefix = "info")
public class InfoConfig {
    private String address;
    private String company;
    private String degree;

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }

    public String getCompany() {
        return company;
    }

    public void setCompany(String company) {
        this.company = company;
    }

    public String getDegree() {
        return degree;
    }

    public void setDegree(String degree) {
        this.degree = degree;
    }
}

读取指定文件

资源目录下建立config/db-config.properties:

db.username=root

db.password=123456

  1. @PropertySource+@Value注解读取方式
@Component
@PropertySource(value =  {
    "config/db-config.properties"}
)
public class DBConfig1 {
    @Value("${db.username}")
    private String username;
    @Value("${db.password}")
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

注意:@PropertySource不支持yml文件读取。

2.@PropertySource+@ConfigurationProperties注解读取方式

@Component
@ConfigurationProperties(prefix = "db")
@PropertySource(value = {"config/db-config.properties"})
public class DBConfig2 {
    private String username;
    private String password;

    public String getUsername() {
        return username;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public String getPassword() {
        return password;
    }

    public void setPassword(String password) {
        this.password = password;
    }
}

3.Environment读取方式

@Autowired
 private Environment environment;

 environment.getProperty("info.degree");

从以上示例来看,Spring Boot可以通过@PropertySource,@Value,@Environment,@ConfigurationProperties的相互配合来读取配置文件的内容,读取单个的配置Environment无疑是最霸道的方法。

最后更新于 2018-05-04 15:14:27 并被添加「java spring boot」标签,已有 8 位童鞋阅读过。

原文  https://www.ydstudio.net/archives/53.html
正文到此结束
Loading...