经常碰见这问题,就记录一下吧
起因
首先,我想要在工具类的静态方法里面调用获取这个属性,所以我把这个改成static
修饰的,重新生成了set,get方法,然后就获取不到了
原因
@ConfigurationProperties只会调用 非静态的set方法
解决方法
将set方法改成非静态
实例代码
package com.ruoyi.common.config;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
/**
* 七牛云相关配置
* @author surfingCat
* @date 2023/9/19 14:57
*/
@Component
@ConfigurationProperties(prefix = "qiniuyun")
public class QiNiuYunConfig {
private static String accessKey;
private static String secretKey;
private static String bucket;
public static String getAccessKey() {
return accessKey;
}
public void setAccessKey(String accessKey) {
QiNiuYunConfig.accessKey = accessKey;
}
public static String getSecretKey() {
return secretKey;
}
public void setSecretKey(String secretKey) {
QiNiuYunConfig.secretKey = secretKey;
}
public static String getBucket() {
return bucket;
}
public void setBucket(String bucket) {
QiNiuYunConfig.bucket = bucket;
}
}
评论区