1. 程式人生 > >Springboot使用@ConfigurationProperties註解 配置讀不進去

Springboot使用@ConfigurationProperties註解 配置讀不進去

首先寫依賴

1 <dependency>
2      <groupId>org.springframework.boot</groupId>
3     <artifactId>spring-boot-configuration-processor</artifactId>
4     <optional>true</optional>
5 </dependency>

@ConfigurationProperties註解的類如下

@ConfigurationProperties(prefix = "club.wenfan.security.browser")
public class SecurityProperties { private VaidateCodeProperties code = new VaidateCodeProperties(); public VaidateCodeProperties getCode() { return code; } public void setCode(VaidateCodeProperties code) { this.code = code; } }
VaidateCodeProperties類如下 
package club.wenfan.security.core.properties;

/** * @author:wenfan * @description: * @data: 2019/1/1 9:31 */ public class VaidateCodeProperties { private ImgCodeProperties img=new ImgCodeProperties(); public ImgCodeProperties getImg() { return img; } public void setImg(ImgCodeProperties img) { this.img = img; } }
ImgCodeProperties 類如下
package club.wenfan.security.core.properties;

/**
 * @author:wenfan
 * @description:
 * @data: 2019/1/1 9:21
 */
public class ImgCodeProperties {

    private  int width = 100;// 定義圖片的width
    private  int height= 30;// 定義圖片的height
    private  int codeCount = 4;// 定義圖片上顯示驗證碼的個數
    private int  expiredTime = 60;

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public int getCodeCount() {
        return codeCount;
    }

    public void setCodeCount(int codeCount) {
        this.codeCount = codeCount;
    }

    public int getExpiredTime() {
        return expiredTime;
    }

    public void setExpiredTime(int expiredTime) {
        this.expiredTime = expiredTime;
    }
}

配置檔案如下

club.wenfan.security.browser.code.img.height=100
club.wenfan.security.browser.code.img.width=100
club.wenfan.security.browser.code.img.codeCount=4
club.wenfan.security.browser.code.img.expiredTime=60

經過多次嘗試,發現:物件名一定要和 Set、 Get 的一致,不然一致導致配置檔案的資訊讀取不到。