SpringBoot中注解@ConfigurationProperties的作用

作用

@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(
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值