yml文件
复制代码
1
2
3web: uploadPath: ${web.upload_path} platformUrl: ${web.platform_url}
在代码中通过@Value使用yml中给的值
复制代码
1
2@Value("${web.uploadPath}") private String uploadpath;
因为yml文件不能通过@VALUE获取,启动项目会报错
复制代码
1
2
3org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'yssCoursePortalController': Injection of autowired dependencies failed; nested exception is java.lang.IllegalArgumentException: Could not resolve placeholder 'web.uploadpath' in string value "${web.uploadpath}" Caused by: java.lang.IllegalArgumentException: Could not resolve placeholder 'web.uploadpath' in string value "${web.uploadpath}"
使用实体类
复制代码
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57package com.xinlianpu.village.utils; import org.springframework.boot.context.properties.ConfigurationProperties; import org.springframework.stereotype.Component; import java.util.ArrayList; import java.util.Collections; import java.util.List; @Component @ConfigurationProperties(prefix = "web") public class YmlValueUtil { private String uploadPath; private String platformUrl; private final Security security = new Security(); public String getUploadPath() { return uploadPath; } public void setUploadPath(String uploadPath) { this.uploadPath = uploadPath; } public String getPlatformUrl() { return platformUrl; } public void setPlatformUrl(String platformUrl) { this.platformUrl = platformUrl; } public Security getSecurity() { return security; } public static class Security{ private String userName; private List<String> roles = new ArrayList<>(Collections.singleton("USER")); public String getUserName() { return userName; } public void setUserName(String userName) { this.userName = userName; } public List<String> getRoles() { return roles; } public void setRoles(List<String> roles) { this.roles = roles; } } }
在其它文件中使用
注入
复制代码
1
2@Autowired private YmlValueUtil ymlValueUtil;
获取使用
复制代码
1File file = new File(ymlValueUtil.getUploadPath() + "/course" + course.getId() + ".html");
最后
以上就是落寞睫毛膏最近收集整理的关于获取YML文件中的值的全部内容,更多相关获取YML文件中内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复