Spring Boot读取配置的几种方式

读取application文件

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

info.address=USA

info.company=Spring

info.degree=high

@Value注解读取方式

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

  2. import org.springframework.stereotype.Component;

  3.  

  4. @Component

  5. publicclassInfoConfig1{

  6.  

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

  8.    privateString address;

  9.  

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

  11.    privateString company;

  12.  

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

  14.    privateString degree;

  15.  

  16.    publicString getAddress(){

  17.        return address;

  18.    }

  19.  

  20.    publicvoid setAddress(String address){

  21.        this.address = address;

  22.    }

  23.  

  24.    publicString getCompany(){

  25.        return company;

  26.    }

  27.  

  28.    publicvoid setCompany(String company){

  29.        this.company = company;

  30.    }

  31.  

  32.    publicString getDegree(){

  33.        return degree;

  34.    }

  35.  

  36.    publicvoid setDegree(String degree){

  37.        this.degree = degree;

  38.    }

  39.  

  40. }

@ConfigurationProperties注解读取方式

  1. @Component

  2. @ConfigurationProperties(prefix ="info")

  3. publicclassInfoConfig2{

  4.  

  5.    privateString address;

  6.    privateString company;

  7.    privateString degree;

  8.  

  9.    publicString getAddress(){

  10.        return address;

  11.    }

  12.  

  13.    publicvoid setAddress(String address){

  14.        this.address = address;

  15.    }

  16.  

  17.    publicString getCompany(){

  18.        return company;

  19.    }

  20.  

  21.    publicvoid setCompany(String company){

  22.        this.company = company;

  23.    }

  24.  

  25.    publicString getDegree(){

  26.        return degree;

  27.    }

  28.  

  29.    publicvoid setDegree(String degree){

  30.        this.degree = degree;

  31.    }

  32.  

  33. }

读取指定文件

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

db.username=root

db.password=123456

@PropertySource+@Value注解读取方式

  1. @Component

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

  3. publicclassDBConfig1{

  4.  

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

  6.    privateString username;

  7.  

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

  9.    privateString password;

  10.  

  11.    publicString getUsername(){

  12.        return username;

  13.    }

  14.  

  15.    publicvoid setUsername(String username){

  16.        this.username = username;

  17.    }

  18.  

  19.    publicString getPassword(){

  20.        return password;

  21.    }

  22.  

  23.    publicvoid 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. publicclassDBConfig2{

  5.  

  6.    privateString username;

  7.    privateString password;

  8.  

  9.    publicString getUsername(){

  10.        return username;

  11.    }

  12.  

  13.    publicvoid setUsername(String username){

  14.        this.username = username;

  15.    }

  16.  

  17.    publicString getPassword(){

  18.        return password;

  19.    }

  20.  

  21.    publicvoid setPassword(String password){

  22.        this.password = password;

  23.    }

  24.  

  25. }

Environment读取方式

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

  1. @Autowired

  2. privateEnvironment env;

  3.  

  4. // 获取参数

  5. String getProperty(String key);

总结

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

猜你喜欢

转载自aoyouzi.iteye.com/blog/2422837
今日推荐