代码中常量的抽取和配置的解耦

1.对于常量的抽取

····比如我们要往redis中存一个手机验证码,如果是注册的话,我们一般是使用业务键加上手机号,那么这个业务键就可以抽取出来。

public class AllConstants {

    // 基本常量
    public class BaseConstants {
        public static final long SECONDS_ONE_MINUTES = 60;
        public static final long MILLIS_ONE_MINUTES = 60000;
        public static final long MILLIS_FIVE_MINUTES = 300000;
    }

    // 前缀常量
    public class PreConstants {
        // 短信验证码
        public static final String KEY_SMS_PREFIX = "register:%s";
        //public static final String KEY_SMS_PREFIX = "register:%s,,,%s";
    }
}

取值通过

String smsKey = String.format(AllConstants.PreConstants.KEY_SMS_PREFIX, 手机号);
可以使用idea输入AllConstants.PreConstants.KEY_SMS_PREFIX.format可以自动提示转换成上面的代码
smsKey的结果就是"register:手机号"。

2.对于配置的抽取

yaml文件

verify:
  img:
    length: 4
    expire: 60
    weight: 140
    height: 40
  sms:
    length: 6
    expire: 300

配置类取值,@Value(“${verify.img.length}”)去单个;
我们取多个使用@ConfigurationProperties

@ConfigurationProperties(prefix = "verify.img")
@Component
@Data
public class ImgProperties {
    public Integer length;
    public Long expire;
    public Integer weight;
    public Integer height;
}
@ConfigurationProperties(prefix = "verify.sms")
@Component
@Data
public class SmsProperties {
    public Integer length;
    public Long expire;
}

使用直接通过@AutoWired注入,然后调用get方法取值就可以。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值