springBoot读取配置文件

Spring Boot 可以通过 @PropertySource,@Value,@Environment, @ConfigurationProperties 来绑定变量,具体请看这篇文章《Spring Boot读取配置的几种方式》。

 

读取application文件


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

 
  1. info.address=USA

  2. info.company=Spring

  3. info.degree=high

 

@Value注解读取方式


 
  1. import org.springframework.beans.factory.annotation.Value;

  2. import org.springframework.stereotype.Component;

  3.  
  4. @Component

  5. public class InfoConfig1 {

  6.  
  7.    @Value("${info.address}")

  8.    private String address;

  9.  
  10.    @Value("${info.company}")

  11.    private String company;

  12.  
  13.    @Value("${info.degree}")

  14.    private String degree;

  15.  
  16.    public String getAddress() {

  17.        return address;

  18.    }

  19.  
  20.    public void setAddress(String address) {

  21.        this.address = address;

  22.    }

  23.  
  24.    public String getCompany() {

  25.        return company;

  26.    }

  27.  
  28.    public void setCompany(String company) {

  29.        this.company = company;

  30.    }

  31.  
  32.    public String getDegree() {

  33.        return degree;

  34.    }

  35.  
  36.    public void setDegree(String degree) {

  37.        this.degree = degree;

  38.    }

  39.  
  40. }

 

@ConfigurationProperties注解读取方式


 
  1. @Component

  2. @ConfigurationProperties(prefix = "info")

  3. public class InfoConfig2 {

  4.  
  5.    private String address;

  6.    private String company;

  7.    private String degree;

  8.  
  9.    public String getAddress() {

  10.        return address;

  11.    }

  12.  
  13.    public void setAddress(String address) {

  14.        this.address = address;

  15.    }

  16.  
  17.    public String getCompany() {

  18.        return company;

  19.    }

  20.  
  21.    public void setCompany(String company) {

  22.        this.company = company;

  23.    }

  24.  
  25.    public String getDegree() {

  26.        return degree;

  27.    }

  28.  
  29.    public void setDegree(String degree) {

  30.        this.degree = degree;

  31.    }

  32.  
  33. }

 

读取指定文件


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

 
  1. db.username=root

  2. db.password=123456

@PropertySource+@Value注解读取方式

 
  1. @Component

  2. @PropertySource(value = { "config/db-config.properties" })

  3. public class DBConfig1 {

  4.  
  5.    @Value("${db.username}")

  6.    private String username;

  7.  
  8.    @Value("${db.password}")

  9.    private String password;

  10.  
  11.    public String getUsername() {

  12.        return username;

  13.    }

  14.  
  15.    public void setUsername(String username) {

  16.        this.username = username;

  17.    }

  18.  
  19.    public String getPassword() {

  20.        return password;

  21.    }

  22.  
  23.    public void setPassword(String password) {

  24.        this.password = password;

  25.    }

  26.  
  27. }

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

 

@PropertySource+@ConfigurationProperties注解读取方式

 
  1. @Component

  2. @ConfigurationProperties(prefix = "db")

  3. @PropertySource(value = { "config/db-config.properties" })

  4. public class DBConfig2 {

  5.  
  6.    private String username;

  7.    private String password;

  8.  
  9.    public String getUsername() {

  10.        return username;

  11.    }

  12.  
  13.    public void setUsername(String username) {

  14.        this.username = username;

  15.    }

  16.  
  17.    public String getPassword() {

  18.        return password;

  19.    }

  20.  
  21.    public void setPassword(String password) {

  22.        this.password = password;

  23.    }

  24.  
  25. }

 

Environment读取方式


以上所有加载出来的配置都可以通过Environment注入获取到。

 
  1. @Autowired

  2. private Environment env;

  3.  
  4. // 获取参数

  5. String getProperty(String key);

 

总结


从以上示例来看,Spring Boot可以通过@PropertySource,@Value,@Environment,@ConfigurationProperties来绑定变量。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值