在春季启动项目中如何实现自定义PropertySourceLoader

  介绍

今天就跟大家聊聊有关在春季启动项目中如何实现自定义PropertySourceLoader,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

SpringBoot的配置文件内置支持属性、xml、yml、yaml几种格式,其中属性和xml对应的装入器类为PropertiesPropertySourceLoader, yml和yaml对应的装入器类为YamlPropertySourceLoader。

观察这2个类可以发现,都实现自接口PropertySourceLoader,所以我们要新增支持别的格式的配置文件,就可以通过实现接口PropertySourceLoader来实现了。

下面实现了一个json格式的配置文件装入器类:

包com.shanhy.sboot.property;
  
  进口java.io.IOException;
  进口java.io.InputStream;
  进口java.util.HashMap;
  进口java.util.LinkedList;
  进口并不知道;
  进口java.util.Map;
  
  进口org.springframework.boot.env.PropertySourceLoader;
  进口org.springframework.boot.json.JsonParser;
  进口org.springframework.boot.json.JsonParserFactory;
  进口org.springframework.core.env.MapPropertySource;
  进口org.springframework.core.env.PropertySource;
  进口org.springframework.core.io.Resource;/* *
  * JSON格式配置文件加载器
  *
  * @author单红宇(CSDN CATOOP)
  * @create 2017年4月20日
  */公共类JsonPropertySourceLoader实现PropertySourceLoader {
  
  公共String [] getFileExtensions () {//配置文件格式(扩展名)
  返回新String [] {“json"};
  }
  
  ,公共PropertySource<和# 63的在负载(字符串名称、资源资源字符串)抛出IOException {//处理机制参考PropertiesPropertySourceLoader//无论资料有没有值,底层都会尝试先执行负载(字符串名称、资源资源,null),所以这个地方之间判断等于零即可。//当前版本springboot-1.5.2(后续版本未知)详见ConfigFileApplicationListener的445行
  如果(profile==null) {
  Object> Map<字符串;结果=mapPropertySource(资源);
  返回新MapPropertySource(名称、结果);
  }
  返回null;
  }/* *
  *解析资源为地图
  *
  * @param资源
  * @return
  * @throws IOException
  *
  * @author单红宇(CSDN CATOOP)
  * @create 2017年4月20日
  */私人Map<字符串,Object>mapPropertySource(资源资源)抛出IOException {
  如果(资源==null) {
  返回null;
  }
  Object> Map<字符串;结果=new HashMap<字符串,Object> ();
  JsonParser解析器=JsonParserFactory.getJsonParser ();
  Object> Map<字符串;地图=parser.parseMap (readFile(资源));
  nestMap(““,因此,地图);
  返回结果;
  }/* *
  *读取资源文件内容为字符串
  *
  * @param资源
  * @return
  * @throws IOException
  *
  * @author单红宇(CSDN CATOOP)
  * @create 2017年4月20日
  */私人字符串readFile(资源资源)抛出IOException {
  InputStream InputStream=resource.getInputStream ();
  ListbyteList=new LinkedList ();
  byte [] readByte=新字节[1024];
  int长度;
  在((长度=read (readByte))比;0) {
  for (int i=0;我& lt;长度;我+ +){
  byteList.add (readByte[我]);
  }
  }
  byte [] allBytes=new byte [byteList.size ());
  int指数=0;
  (字节soloByte: byteList) {
  allBytes(指数)=soloByte;
  指数+=1;
  }
  返回新字符串(allBytes,“UTF-8");
  }/* *
  *处理地图(地图中可能还嵌套映射,递归处理),最终输出一个非嵌套的地图
  *
  * @param前缀
  *前缀
  * @param结果
  *处理后的地图
  * @param地图
  *处理前的地图
  *
  * @author单红宇(CSDN CATOOP)
  * @create 2017年4月20日
  */@SuppressWarnings (“unchecked")
  私人空间nestMap(字符串前缀,Map<字符串,Object>因此,Map<字符串,Object>地图){
  如果(prefix.length()在0) {
  前缀+=?”
  }
  (Map.Entry<字符串,Object>entrySet: map.entrySet ()) {
  如果(entrySet.getValue()运算符地图){
  nestMap(前缀+ entrySet.getKey(),因此,(Map<字符串,Object>) entrySet.getValue ());
  其他}{
  结果。把(前缀+ entrySet.getKey () .toString (), entrySet.getValue ());
  }
  }
  }
  }

然后在src/main/资源中创建meta - inf/spring。工厂文件,内容为:

org.springframework.boot.env.PropertySourceLoader=\   com.shanhy.sboot.property。JsonPropertySourceLoader

创建测试的配置文件的应用程序。json

{   “custom": {   “property": {   “message":“测试数据“;   }   }   }

在春季启动项目中如何实现自定义PropertySourceLoader