Springboot使用@ConfigurationProperties注解 配置读不进去

Posted wenfan

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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 的一致,不然一致导致配置文件的信息读取不到。

 

以上是关于Springboot使用@ConfigurationProperties注解 配置读不进去的主要内容,如果未能解决你的问题,请参考以下文章

微服务框架面试题

Eclipse编写SpringBoot中的Application.yml没有任何提示

Spring Boot:加载DataSource过程的源码分析及yml中DataSource的配置

Consider defining a bean of type ‘com.example.springbootdemo.mapper.UserMapper‘ in your configuratio

Consider defining a bean of type ‘com.example.springbootdemo.mapper.UserMapper‘ in your configuratio

Spring Security getAuthentication() 返回 null