springboot @ConfigurationProperties与静态加载yml文件时取值为null

通过@ConfigurationProperties注解则会将值映射到该类中

yml配置:

# 项目相关配置
singcell:
  # 名称
  name: jhhs
  # 版本
  version: 1.1.0
  # 版权年份
  copyrightYear: 2020
  # 实例演示开关
  demoEnabled: true
  # 文件路径 示例( Windows配置c:/gene/filePath,Linux配置 /home/gene/filePath)
  profile: c:/gene/filePath
  # 图片路径 示例( Windows配置c:/gene/imagePath,Linux配置 /home/gene/imagePath)
  imageUrl: c:/gene/imagePath
  # 样本文件路径
  sampleSheets: c:/gene/sampleSheets
  # 获取ip地址开关
  addressEnabled: false
  # 验证码类型 math 数组计算 char 字符验证
  captchaType: math

遇到的问题:

    在获取profile的时候,值始终都是null

错误代码:

@Component
@ConfigurationProperties(prefix = "singcell")
public class TossConfig {

    /**文件上传路径*/
    private static String profile;

    public static String getProfile() {
        return profile;
    }

    public static void setProfile(String profile) {
        TossConfig.profile = profile;
    }

    public static String getDownloadPath(){
        return profile + "download/";
    }
}

项目运行后发现 profile取值是 null;
这是因为@ConfigurationProperties只会调用 非静态的set方法,所以稍做改动,set方法都换成非静态的,贴下正确的代码:

@Component
@ConfigurationProperties(prefix = "toss")
public class TossConfig {

    /**文件上传路径*/
    private static String profile;

    public static String getProfile() {
        return profile;
    }

    public void setProfile(String profile) {
        TossConfig.profile = profile;
    }

    public static String getDownloadPath(){
        return profile + "download/";
    }
}
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值