首先写依赖
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 的一致,不然一致导致配置文件的信息读取不到。