spring中将配置文件的属性注入到静态属性里

场景

如在工具类中,不想将该工具类被spring管理,通过引用其他类的的静态属性获取值。

步骤

application.properties

1
2
3
4
5
6
#局域网地址
file.lanServer=\\\\DESKTOP-3FE3ORR\\app\\
#app下载地址
file.remoteServer=http://127.0.0.1:8080/app/
file.fileUploadDir=H:/vadx
file.relatePath=/vadx/app

新建类, 如文件路径配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
@Component
@Getter
@Setter
public class FileProp {

public static String remoteServer;
public static String lanServer;
public static String fileUploadDir;
public static String relatePath;

@Value("${file.remoteServer}")
public void setRemoteServer(String remoteServer) {
FileProp.remoteServer = remoteServer;
}

@Value("${file.lanServer}")
public void setLanServer(String lanServer) {
FileProp.lanServer = lanServer;
}

@Value("${file.fileUploadDir}")
public void setFileUploadDir(String fileUploadDir) {
FileProp.fileUploadDir = fileUploadDir;
}

@Value("${file.relatePath}")
public void setRelatePath(String relatePath) {
FileProp.relatePath = relatePath;
}
}

使用,在工具类中使用

1
2
3
4
5
6
@Slf4j
public class UploadFileUtil {

//项目前缀路径
public static String destFile = FileProp.fileUploadDir;
}
# spring

评论

Your browser is out-of-date!

Update your browser to view this website correctly. Update my browser now

×