• 使用spring的DefaultResourceLoader自定义properties文件加载工具类


    转自;https://www.cnblogs.com/zrbfree/p/6230957.html

      1 import java.io.IOException;
      2 
      3 import java.io.InputStream;
      4 
      5 import java.util.NoSuchElementException;
      6 
      7 import java.util.Properties;
      8 
      9  
     10 
     11 import org.apache.commons.io.IOUtils;
     12 
     13 import org.slf4j.Logger;
     14 
     15 import org.slf4j.LoggerFactory;
     16 
     17 import org.springframework.core.io.DefaultResourceLoader;
     18 
     19 import org.springframework.core.io.Resource;
     20 
     21 import org.springframework.core.io.ResourceLoader;
     22 
     23  
     24 
     25 /**
     26 
     27  * Properties文件载入工具类. 可载入多个properties文件, *相同的属性在最后载入的文件中的值将会覆盖之前的值,但以System的Property优先.
     28 
     29  */
     30 
     31 public class PropertiesLoader {
     32 
     33  
     34 
     35 private static Logger logger = LoggerFactory.getLogger(PropertiesLoader.class);
     36 
     37  
     38 
     39 private static ResourceLoader resourceLoader = new DefaultResourceLoader();
     40 
     41  
     42 
     43 private final Properties properties;
     44 
     45  
     46 
     47 public PropertiesLoader(String... resourcesPaths) {
     48 
     49 properties = loadProperties(resourcesPaths);
     50 
     51 }
     52 
     53  
     54 
     55 public Properties getProperties() {
     56 
     57 return properties;
     58 
     59 }
     60 
     61  
     62 
     63 /**
     64 
     65  * 取出Property,但以System的Property优先,取不到返回空字符串.
     66 
     67  */
     68 
     69 private String getValue(String key) {
     70 
     71 String systemProperty = System.getProperty(key);
     72 
     73 if (systemProperty != null) {
     74 
     75 return systemProperty;
     76 
     77 }
     78 
     79 if (properties.containsKey(key)) {
     80 
     81         return properties.getProperty(key);
     82 
     83     }
     84 
     85     return "";
     86 
     87 }
     88 
     89  
     90 
     91 /**
     92 
     93  * 取出String类型的Property,但以System的Property优先,如果都为Null则抛出异常.
     94 
     95  */
     96 
     97 public String getProperty(String key) {
     98 
     99 String value = getValue(key);
    100 
    101 if (value == null) {
    102 
    103 throw new NoSuchElementException();
    104 
    105 }
    106 
    107 return value;
    108 
    109 }
    110 
    111  
    112 
    113 /**
    114 
    115  * 取出String类型的Property,但以System的Property优先.如果都为Null则返回Default值.
    116 
    117  */
    118 
    119 public String getProperty(String key, String defaultValue) {
    120 
    121 String value = getValue(key);
    122 
    123 return value != null ? value : defaultValue;
    124 
    125 }
    126 
    127  
    128 
    129 /**
    130 
    131  * 取出Integer类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
    132 
    133  */
    134 
    135 public Integer getInteger(String key) {
    136 
    137 String value = getValue(key);
    138 
    139 if (value == null) {
    140 
    141 throw new NoSuchElementException();
    142 
    143 }
    144 
    145 return Integer.valueOf(value);
    146 
    147 }
    148 
    149  
    150 
    151 /**
    152 
    153  * 取出Integer类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
    154 
    155  */
    156 
    157 public Integer getInteger(String key, Integer defaultValue) {
    158 
    159 String value = getValue(key);
    160 
    161 return value != null ? Integer.valueOf(value) : defaultValue;
    162 
    163 }
    164 
    165  
    166 
    167 /**
    168 
    169  * 取出Double类型的Property,但以System的Property优先.如果都为Null或内容错误则抛出异常.
    170 
    171  */
    172 
    173 public Double getDouble(String key) {
    174 
    175 String value = getValue(key);
    176 
    177 if (value == null) {
    178 
    179 throw new NoSuchElementException();
    180 
    181 }
    182 
    183 return Double.valueOf(value);
    184 
    185 }
    186 
    187  
    188 
    189 /**
    190 
    191  * 取出Double类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容错误则抛出异常
    192 
    193  */
    194 
    195 public Double getDouble(String key, Integer defaultValue) {
    196 
    197 String value = getValue(key);
    198 
    199 return value != null ? Double.valueOf(value) : defaultValue;
    200 
    201 }
    202 
    203  
    204 
    205 /**
    206 
    207  * 取出Boolean类型的Property,但以System的Property优先.如果都为Null抛出异常,如果内容不是true/false则返回false.
    208 
    209  */
    210 
    211 public Boolean getBoolean(String key) {
    212 
    213 String value = getValue(key);
    214 
    215 if (value == null) {
    216 
    217 throw new NoSuchElementException();
    218 
    219 }
    220 
    221 return Boolean.valueOf(value);
    222 
    223 }
    224 
    225  
    226 
    227 /**
    228 
    229  * 取出Boolean类型的Property,但以System的Property优先.如果都为Null则返回Default值,如果内容不为true/false则返回false.
    230 
    231  */
    232 
    233 public Boolean getBoolean(String key, boolean defaultValue) {
    234 
    235 String value = getValue(key);
    236 
    237 return value != null ? Boolean.valueOf(value) : defaultValue;
    238 
    239 }
    240 
    241  
    242 
    243 /**
    244 
    245  * 载入多个文件, 文件路径使用Spring Resource格式.
    246 
    247  */
    248 
    249 private Properties loadProperties(String... resourcesPaths) {
    250 
    251 Properties props = new Properties();
    252 
    253 for (String location : resourcesPaths) {
    254 
    255 logger.debug("Loading properties file from:" + location);
    256 
    257 InputStream is = null;
    258 
    259 try {
    260 
    261 Resource resource = resourceLoader.getResource(location);
    262 
    263 is = resource.getInputStream();
    264 
    265 props.load(is);
    266 
    267 } catch (IOException ex) {
    268 
    269 logger.info("Could not load properties from path:" + location + ", " + ex.getMessage());
    270 
    271 } finally {
    272 
    273 IOUtils.closeQuietly(is);
    274 
    275 }
    276 
    277 }
    278 
    279 return props;
    280 
    281 }
    282 
    283 }
  • 相关阅读:
    Markdown高级使用之流程图
    Sentinel滑动窗口算法
    Markdown基础使用
    多线程学习(二)--整体简介
    MYSQL学习(三) --索引详解
    MYSQL学习(二) --MYSQL框架
    MYSQL 学习(一)--启蒙篇《MYSQL必知必会》
    数据结构学习(六) --排序
    数据结构学习(五)--查找
    数据结构学习(四)--图
  • 原文地址:https://www.cnblogs.com/sharpest/p/9952303.html
Copyright © 2020-2023  润新知