读取配置文件properties

本文介绍了一个用于加载和管理配置文件的Java类HosConfiguration。该类通过PathMatchingResourcePatternResolver加载所有class路径下的属性文件,并将它们统一存放在Properties对象中供应用程序使用。
 1 package com.imooc.bigdata.hos.core;
 2 
 3 import java.io.IOException;
 4 import java.io.InputStream;
 5 import java.util.Properties;
 6 
 7 import org.springframework.core.io.Resource;
 8 import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
 9 
10 /**
11  * Created by jixin on 18-3-8.
12  */
13 public class HosConfiguration {
14 
15   private static HosConfiguration configuration;
16   private static Properties properties;
17 
18   static {
19 
20     PathMatchingResourcePatternResolver resourceLoader = new PathMatchingResourcePatternResolver();
21     configuration = new HosConfiguration();
22     try {
23       configuration.properties = new Properties();
24       Resource[] resources = resourceLoader.getResources("classpath:*.properties");
25       for (Resource resource : resources) {
26         Properties props = new Properties();
27         InputStream input = resource.getInputStream();
28         props.load(input);
29         input.close();
30         configuration.properties.putAll(props);
31       }
32     } catch (IOException e) {
33       e.printStackTrace();
34     }
35 
36   }
37 
38   private HosConfiguration() {
39   }
40 
41   public static HosConfiguration getConfiguration() {
42     return configuration;
43   }
44 
45 
46   public String getString(String key) {
47     return properties.getProperty(key);
48   }
49 
50   public int getInt(String key) {
51     return Integer.parseInt(properties.getProperty(key));
52   }
53 
54   public boolean getBoolean(String key) {
55     return Boolean.parseBoolean(properties.getProperty(key));
56   }
57 
58   public long getLong(String key) {
59     return Long.parseLong(properties.getProperty(key));
60   }
61 
62 }

 

转载于:https://www.cnblogs.com/xjatj/p/9366813.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值