作用
@ConfigurationProperties的作用:可以读取配置文件中的信息,并自动封装成实体类,该实体类的名称,默认是类名的首字母小写。
比如,在application.yml中有这样的内容:
# 安全配置
security:
# 验证码
captcha:
enabled: true
type: math
代码演示
package com.ruoyi.gateway.config.properties;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Configuration;
/**
* 验证码配置
*/
@Configuration
@RefreshScope
@ConfigurationProperties(prefix = "security.captcha")
public class CaptchaProperties{
/**
* 验证码开关
*/
private Boolean enabled;
/**
* 验证码类型(math 数组计算 char 字符)
*/
private String type;
public Boolean getEnabled(){
return enabled;
}
public void setEnabled(Boolean enabled){
this.enabled = enabled;
}
public String getType(){
return type;
}
public void setType(String type){
this.type = type;
}
}
@RefreshScope :开启自动刷新功能。
@ConfigurationProperties(prefix = “security.captcha”) :在配置文件中,找寻路径为security.captcha的配置信息,并读取其下的配置项,与类中的字段进行匹配。
CaptchaProperties如何使用
CaptchaConfig 验证码配置
package com.ruoyi.gateway.config;
import com.google.code.kaptcha.impl.DefaultKaptcha;
import com.google.code.kaptcha.util.Config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import java.util.Properties;
import static com.google.code.kaptcha.Constants.*;
/**
* 验证码配置
*/
@Configuration
public class CaptchaConfig
{
@Bean(name = "captchaProducer")
public DefaultKaptcha getKaptchaBean(