@Value annotation static variable assignment in Springboot

The configuration file application.properties has the following configuration:

file.dir = /home/zhbr/fileUpload

There is a fileDir static variable in a tool class:

private static String fileDir;

When dealing with static variables, if the @Value annotation is used directly, the data in the configuration file cannot be obtained, and the final value of fileDir is null.

To assign a value to this static variable through the configuration file:

1️⃣Use IDEA to generate the set method of the static variable, and then delete the static modification of the method
2️⃣Then write the annotation @Value on the set function

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

Guess you like

Origin blog.csdn.net/weixin_44455388/article/details/121702804