常用三种方法:
1、@Value注解读取
2、使用Environment读取
3、使用@ConfigurationProperties注解读取
代码:
链接:https://pan.baidu.com/s/1g56ZUu5Xkf07pNqyHprZBw
提取码:d155
重点说下第三种方法,@ConfigurationProperties项目中用的比较多,其他两种参考:
https://blog.csdn.net/dkbnull/article/details/81953190?spm=1001.2101.3001.6650.3&depth_1-
在src\main\resources下新建config.properties配置文件:
demo.phone=10086
demo.wife=self
1
2
ConfigBeanProp文件
@Component
@ConfigurationProperties(prefix = "demo")
@PropertySource(value = "config.properties")
public class ConfigBeanProp {
private String phone;
private String wife;
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getWife() {
return wife;
}
public void setWife(String wife) {
this.wife = wife;
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
@Component 表示将该类标识为Bean
@ConfigurationProperties(prefix = “demo”)用于绑定属性,其中prefix表示所绑定的属性的前缀。
@PropertySource(value = “config.properties”)表示配置文件路径。
@RestController
public class GatewayController {
@Value("${demo.name}")
private String name;
@Value("${demo.age}")
private String age;
@Autowired
private ConfigBeanValue configBeanValue;
@Autowired
private Environment environment;
@Autowired
private ConfigBeanProp configBeanProp;
@RequestMapping(value = "/gateway4")
public String gateway4() {
return "get properties value by ''@Value'' :" +
//1、使用@Value注解读取
" name=" + configBeanValue.name +
" , age=" + configBeanValue.age +
"<p>get properties value by ''Environment'' :" +
//2、使用Environment读取
" sex=" + environment.getProperty("demo.sex") +
" , address=" + environment.getProperty("demo.address") +
"<p>get properties value by ''@ConfigurationProperties'' :" +
//3、使用@ConfigurationProperties注解读取
" phone=" + configBeanProp.getPhone() +
" , wife=" + configBeanProp.getWife();
}
}
二、扩展
SpringBoot读取配置文件中定义的数组集合
1、application.yml配置文件
navigation:
platforminfo:
- key: "fusion"
name: "大数据融合平台"
- key: "open"
name : "开放云服务平台"
navigationbar:
applinks:
- name: "数据概览"
key: "fusion.home"
url: "/fusion/home/v1/"
- name: "数据集成"
key: "fusion.integration"
url: "/fusion/integration/v1/"
...
model:
public class PlatFormInfo {
private String key;
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
private String name;
}
public class AppLinks {
private String name;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
private String key;
private String url;
}
43
Properties:
@Component
@ConfigurationProperties(prefix = "navigationbar" )
public class GlobalLinkProperties {
public List<AppLinks> getAppLinks() {
return appLinks;
}
public void setAppLinks(List<AppLinks> appLinks) {
this.appLinks = appLinks;
}
private List<AppLinks> appLinks = new ArrayList<>();
}
@Component
@ConfigurationProperties(prefix = "navigation" )
public class GlobalNameProperties {
public List<PlatFormInfo> getPlatFormInfo() {
return platFormInfo;
}
public void setPlatFormInfo(List<PlatFormInfo> platFormInfoList) {
this.platFormInfo = platFormInfoList;
}
private List<PlatFormInfo> platFormInfo = new ArrayList<>();
}
25
RestLoginHeaderResponse:
public class RestLoginHeaderResponse {
private List<PlatFormInfo> platFormInfoList = new ArrayList<>();
public List<PlatFormInfo> getPlatFormInfoList() {
return platFormInfoList;
}
public void setPlatFormInfoList(List<PlatFormInfo> platFormInfoList) {
this.platFormInfoList = platFormInfoList;
}
public List<AppLinks> getAppLinksList() {
return appLinksList;
}
public void setAppLinksList(List<AppLinks> appLinksList) {
this.appLinksList = appLinksList;
}
private List<AppLinks> appLinksList = new ArrayList<>();
}
17
测试Controller:
@Controller
public class HeaderController {
@Autowired
private GlobalNameProperties globalNameProperties;
@Autowired
private GlobalLinkProperties globalLinkProperties;
@Autowired
CorsProperties corsProperties;
@RequestMapping( "/test" )
@ResponseBody
public RestResponse<RestLoginHeaderResponse> test(){
RestResponse<RestLoginHeaderResponse> res = new RestResponse<>();
RestLoginHeaderResponse response = new RestLoginHeaderResponse();
List<PlatFormInfo> platFormInfoList = globalNameProperties.getPlatFormInfo();
List<AppLinks> appLinksList = globalLinkProperties.getAppLinks();
response.setPlatFormInfoList(platFormInfoList);
response.setAppLinksList(appLinksList);
res.setBody(response);
return res;
}
}
其中RestResponse是返回前端的response,自己找一个就行
返回json如下:
注意:
ConfigurationProperties 注解用于设置Springboot从配置文件中取值过程中的约束。
定义的List 、List对象名应该和配置文件中的相同。
PlatFormInfo、AppLinks对象的属性名应该和配置文件中的key相同。
即下图要保持一致:
来源:SpringBoot读取properties配置文件中的数据_逍遥浪子~的博客-CSDN博客_springboot 读取properties配置参数