使用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 }

 

转载于:https://www.cnblogs.com/sharpest/p/9952303.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值