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

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

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

1
2
3
info.address=RPC
info.company=Spring
info.degree=high

1.@Value注解读取方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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注解读取方式

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@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注解读取方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    @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注解读取方式

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    @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读取方式

    1
    2
    3
    4
    @Autowired
    private Environment environment;

    environment.getProperty("info.degree");

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

  • 作者: Sam
  • 发布时间: 2018-05-04 21:33:18
  • 最后更新: 2019-12-09 23:03:26
  • 文章链接: https://ydstudios.gitee.io/post/28da4df8.html
  • 版权声明: 本网所有文章除特别声明外, 禁止未经授权转载,违者依法追究相关法律责任!