Springboot中@Value注解静态变量赋值

配置文件application.properties中有以下配置:

file.dir = /home/zhbr/fileUpload

某工具类中存在fileDir静态变量:

private static String fileDir;

在处理静态变量时候,如直接使用@Value注解,是无法获取到配置文件中的数据的,最终fileDir的值为null。

如要通过配置文件对该静态变量进行赋值:

1️⃣利用IDEA生成该静态变量的set方法,然后删除该方法的static修饰
2️⃣然后将注解@Value写在set函数上面

//ConfigConstants为工具类类名
@Value("${file.dir}")
public void setFileDir(String fileDir) {
    ConfigConstants.fileDir = fileDir;
}

猜你喜欢

转载自blog.csdn.net/weixin_44455388/article/details/121702804