SpringBoot用@ConfigurationProperties獲取配置文件值


SpringBoot的配置文件有yml和properties兩種,看一些文章說yml以數據為中心,比較好。個人覺得properties更好用,所以這里以properties格式為例來說。

我們都知道@Value 注解可以從配置文件讀取一個配置,如果只是配置某個值,比如 某一個域名,配置為xxx.domain = www.xxx.com ,這樣直接在代碼里用@Value獲取,比較方便

但是如果是一組相關的配置,比如驗證碼相關的配置,有圖片驗證碼、手機驗證碼、郵箱驗證碼,如果想把驗證碼的長度做成可配置。是否能像springboot的配置,

參照着寫成:

肯定是可以的!

參照源碼是最好的學習方式,下面來看springboot是怎么做的

server對應着一個配置類:ServerProperties(只粘貼出了部分成員變量,來說明問題)
 
        
@ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) public class ServerProperties implements EmbeddedServletContainerCustomizer, EnvironmentAware, Ordered { /** * Server HTTP port. */ private Integer port; @NestedConfigurationProperty  private Compression compression = new Compression(); //省略其他成員變量、getter 、setter 
 
        
Compression類部分代碼:
public class Compression { /** * If response compression is enabled. */ private boolean enabled = false;

 看過后應該很明白了,之所以能寫成server.port=8081,server.display-name=lhyapp,server.compression.enabled=true  ,是因為 ServerProperties 類上使用了

 @ConfigurationProperties(prefix = "server", ignoreUnknownFields = true) 注解,其中prefix 指定配置文件里的前綴, 如果想弄成這樣式的 server.compression.enabled = true ,就需要再聲名一個類 Compression ,然ServerProperties 中引用這個類,屬性名對應配置文件中的配置名。

 

 @ConfigurationProperties

  告訴SpringBoot將本類中的所有屬性和配置文件中相關的配置進行綁定;
   prefix = "xxx":配置文件中哪個下面的所有屬性進行一一映射

 只有這個組件是容器中的組件,才能容器提供的@ConfigurationProperties功能;
 @ConfigurationProperties(prefix = "xxx")默認從全局配置文件中獲取值;

下邊實現上面說的驗證碼配置,需要的類:

代碼:
CoreConfiguration.java
@Configuration
@EnableConfigurationProperties(SecurityProperties.class)
public class CoreConfiguration {

    //配置一些bean
    //@Bean
    //public XXXX xxxx(){}
}
SecurityProperties.java
@ConfigurationProperties(prefix = "myapp")
public class SecurityProperties {

    private ValidateCodeProperties code = new ValidateCodeProperties();

    public ValidateCodeProperties getCode() {
        return code;
    }

    public void setCode(ValidateCodeProperties code) {
        this.code = code;
    }
}
ValidateCodeProperties.java
public class ValidateCodeProperties {

    private SmsCodeProperties sms = new SmsCodeProperties();

    private ImageCodeProperties image = new ImageCodeProperties();

    public SmsCodeProperties getSms() {
        return sms;
    }

    public void setSms(SmsCodeProperties sms) {
        this.sms = sms;
    }

    public ImageCodeProperties getImage() {
        return image;
    }

    public void setImage(ImageCodeProperties image) {
        this.image = image;
    }
}
SmsCodeProperties.java
public class SmsCodeProperties {

    private int length = 4;

    public int getLength() {
        return length;
    }

    public void setLength(int length) {
        this.length = length;
    }
}
在application.properties 里配置
myapp.code.sms.length = 10
使用配置:
  @Autowired private SecurityProperties securityProperties;

    @RequestMapping("/length")
    public @ResponseBody String length(){
        int length = securityProperties.getCode().getSms().getLength();
        return String.valueOf(length);
    }
 
        

 

 
 
 
 
        
 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM