public class ConfigUtil {

private static Map<String,Properties> map = new HashMap<String,Properties>();

/**
* 根据Properties文件名称获取Properties对象
* @param name
* @return Properties
* @throws IOException
*/
private synchronized static Properties createProperties(String name) throws IOException{
Properties p = map.get(name);
if(p == null){
p = new Properties();
p.load(ConfigUtil.class.getResourceAsStream(name));
map.put(name, p);
}
return p;
}
/**
* 根据Properties文件名和其中的key获取value
* @param proName, key
* @return String value
* @throws IOException
*/
public static String getValue(String proName,String key) throws IOException{
return createProperties(proName).getProperty(key);
}
/**
* 根据mqttconfig.properties文件的key获取value
* @param String key
* @return String value
* @throws IOException
*/
public static String getValue(String mqttKey) throws IOException{
return createMQTTProperties().getProperty(mqttKey);
}
/**
* 专门获取mqttconfig.properties的Properties
* @param
* @return Properties p
* @throws IOException
*/
public static Properties createMQTTProperties() throws IOException{
Properties p = map.get("mqttconfig.properties");
if(p == null){
p = new Properties();
p.load(ConfigUtil.class.getResourceAsStream("/mqttconfig.properties"));
map.put("mqttconfig.properties", p);
}
return p;
}
public static void main(String[] args) throws IOException{
System.out.println(getValue("/mqttconfig.properties","port"));
}
}

一个读取propeties配置文件的工具类,线程安全的的更多相关文章

  1. 基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil

    基于Dapper二次封装了一个易用的ORM工具类:SqlDapperUtil,把日常能用到的各种CRUD都进行了简化封装,让普通程序员只需关注业务即可,因为非常简单,故直接贴源代码,大家若需使用可以直 ...

  2. Java Windows下读取注册表的工具类

    import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import ...

  3. 实现一个简单的http请求工具类

    OC自带的http请求用起来不直观,asihttprequest库又太大了,依赖也多,下面实现一个简单的http请求工具类 四个文件源码大致如下,还有优化空间 MYHttpRequest.h(类定义, ...

  4. Java读取Maven工程下的配置文件,工具类

    Java开发中,经常需要在maven工程中读取src/main/resources下的配置文件: 思路如下: Class.getClassLoader() 返回类加载器ClassLoader,进而可以 ...

  5. Java静态static工具类线程安全问题研究

    针对静态方法有以下一些前提: 静态方法和实例方法的区别是静态方法只能引用静态变量,静态方法通过类名来调用,实例方法通过对象实例来调用 每个线程都有自己的线程栈,栈与线程同时创建,每一个虚拟机线程都有自 ...

  6. property_自己编写一个读取Property文件的Util类

    读取property文件的Util类: 所需jar包: 编写PropertiesUtil类: package com.west.util.property; import java.io.InputS ...

  7. 分享一个关于jackson的Json工具类

    直接贴代码: import org.codehaus.jackson.map.DeserializationConfig.Feature; import org.codehaus.jackson.ma ...

  8. 读取properties配置的工具类

    @Service public class AppPropertiesManager implements DisposableBean{ @Value("${shortloan_rate_ ...

  9. 一个简单IP防刷工具类, x秒内最多允许y次单ip操作

    IP防刷,也就是在短时间内有大量相同ip的请求,可能是恶意的,也可能是超出业务范围的.总之,我们需要杜绝短时间内大量请求的问题,怎么处理? 其实这个问题,真的是太常见和太简单了,但是真正来做的时候,可 ...

随机推荐

  1. 【二分】Codeforces 706B Interesting drink

    题目链接: http://codeforces.com/problemset/problem/706/B 题目大意: n (1 ≤ n ≤ 100 000)个商店卖一个东西,每个商店的价格Ai,你有m ...

  2. Android新浪微博客户端(三)——添加多个账户及认证

    原文出自:方杰|http://fangjie.info/?p=72 转载请注明出处 一.微博OAuth2.0认证 首先来说说授权过程,我这里授权是通过SDK的,先添加SDK的jar包,微博SDK的de ...

  3. [LeetCode] 30. Substring with Concatenation of All Words 解题思路 - Java

    You are given a string, s, and a list of words, words, that are all of the same length. Find all sta ...

  4. python用parammiko模块实现linux的远程操作

    parammiko  可以实现远程的带密码登录,解决ssh远程登陆需要交互的问题 (当然很多其他的,如tcl也可以).但这个用python做比较简单 1.parammiko 的安装 1.1.依赖模块 ...

  5. Oracle执行计划——all_rows和first_rows(n) 优化器模式

    0. 环境创建 SQL> create usertest identified by test 2 default tablespace users 3 temporary tablespace ...

  6. Java中可变长参数的方法

    原文转自:http://www.cnblogs.com/lanxuezaipiao/p/3190673.html 在Java5 中提供了变长参数(varargs),也就是在方法定义中可以使用个数不确定 ...

  7. git commit error about 'vi'

    error: There was a problem with the editor 'vi'. Please supply the message using either -m or -F opt ...

  8. JSP具体篇——out

    out对象 out对象用于在web浏览器上输出信息,而且管理应用server上的输出缓冲区.在使用out对象输出数据时.能够对数据缓冲区进行操作.及时清除缓冲区中残留的数据.为其它输出让出缓冲空间. ...

  9. Android 用ping的方法判断当前网络是否可用

    判断网络的情况中,有个比较麻烦的情况就是连上了某个网络,但是那个网络无法上网 ,,, = = 想到了用ping指令来判断,经测试,可行~ ~ ~ private static final boolea ...

  10. leetCode 48.Rotate Image (旋转图像) 解题思路和方法

    Rotate Image You are given an n x n 2D matrix representing an image. Rotate the image by 90 degrees ...