通过@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/";
}
}