SpringBoot零基础入门指南3--读取yml配置文件
删除application.properties并在原地添加
application.yml
application-dev.yml
application-prod.yml
在application.yml中添加spring.profiles.active 选中会数据会自动布局,或者手动布局
并赋值 dev ,此配置为选中启动时的配置文件为application-dev.yml
spring:
profiles:
active: dev
添加一些配置到application-dev.yml里面,需要格式正确
server:
context-path: /
port: 8080
session:
timeout: 30
tomcat:
uri-encoding: utf-8
max-threads: 2000
#### thymeleaf
cache设置为false 界面不重启容器也能更新 #####
spring:
main:
banner-mode: "off" ## 启动banner图取消
thymeleaf:
cache: false
mode: HTML5
##spring-boot-devtools 打包时是否移除
devtools:
remote:
secret: false
然后添加一些需要读取的数据:
#获取数据的测试数据源
ymlData:
stringData: simpleData
arrayData: 1,2,3,4,5,6
listData1:
- name:
abc
value: abcsvalue
- name: efg
value: efgsvalue
listData2:
- list1111
- list2222
mapData:
key1: aa
key2: bb
key3: cc
添加以下文件并添加代码:
YmlController.java
package com.xingquan.controller;
import com.xingquan.service.YmlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/")
public class YmlController {
@Autowired
private YmlService
ymlService;
@RequestMapping("show")
public String
showData() {
ymlService.showData();
return "完成了";
}
}
YmlData.java
package com.xingquan.entity;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;
import java.util.List;
import java.util.Map;
/**
* 注意属性名称和yml配置文件的名称一致
*/
@Component
@ConfigurationProperties(prefix = "ymlData")
public class YmlData {
private String
stringData;
private int[] arrayData;
private List<Map<String, String>>
listData1;
private List<String> listData2;
private Map<String, String> mapData;
public String getStringData() {
return stringData;
}
public void setStringData(String
stringData) {
this.stringData = stringData;
}
public int[] getArrayData() {
return arrayData;
}
public void setArrayData(int[] arrayData) {
this.arrayData = arrayData;
}
public List<Map<String, String>> getListData1() {
return listData1;
}
public void setListData1(List<Map<String, String>> listData1) {
this.listData1 = listData1;
}
public List<String>
getListData2() {
return listData2;
}
public void setListData2(List<String>
listData2) {
this.listData2 = listData2;
}
public Map<String, String> getMapData() {
return mapData;
}
public void setMapData(Map<String, String> mapData) {
this.mapData = mapData;
}
}
YmlService.java
package com.xingquan.service;
public interface YmlService {
void showData();
}
YmlServiceImpl.java
package com.xingquan.service.serviceImpl;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.xingquan.entity.YmlData;
import com.xingquan.service.YmlService;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
@Service
public class YmlServiceImpl implements YmlService{
@Autowired
private YmlData
ymlData;
private Log _log
= LogFactory.getLog(YmlService.class);
@Override
public void showData() {
ObjectMapper objectMapper = new ObjectMapper();
try {
_log.info(ymlData.getStringData());
_log.info(objectMapper.writeValueAsString(ymlData.getArrayData()));
_log.info(objectMapper.writeValueAsString(ymlData.getListData1()));
_log.info(objectMapper.writeValueAsString(ymlData.getListData2()));
_log.info(objectMapper.writeValueAsString(ymlData.getMapData()));
} catch
(JsonProcessingException e) {
e.printStackTrace();
}
}
}
启动tomcat并在浏览器输入http://localhost:8080/show
idea控制台显示