import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties; import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory; public final class AppConfig { private static final Log logger = LogFactory.getLog(AppConfig.class); private static final String FILE_NAME = "xwcg.properties"; private static AppConfig sdkConfig = null; /**
* 网关请求地址
*/
private String xwcgGatewayUrl; /**
* 存管直连地址
*/
private String xwcgServiceUrl; /**
*存管对账文件下载地址
*/
private String xwcgDownloadUrl; /**
* 平台编号
*/
private String platformNo; /**
* 证书编号
*/
private String keySerial; /**
* 平台私钥
*/
private String privateKey; /**
* 平台公钥
*/
private String publicKey; /**
* 私钥
*/
private String lmPrivateKey; /**
* 公钥
*/
private String lmPublicKey; /**
* 连接超时
*/
private int connectionTimeout; /**
* 获取数据超时
*/
private int readTimeout; private AppConfig() {
this.loadPropertiesFromSrc();
} public static AppConfig getConfig() {
synchronized (AppConfig.class) {
if (null == sdkConfig) {
sdkConfig = new AppConfig();
}
return sdkConfig;
}
} private void loadPropertiesFromSrc() {
InputStream in = null;
try {
Properties properties = null;
logger.info("从classpath: "+ AppConfig.class.getClassLoader().getResource("").getPath() + " 获取属性文件" + FILE_NAME);
in = AppConfig.class.getClassLoader().getResourceAsStream(FILE_NAME);
if (null != in) {
BufferedReader bf = new BufferedReader(new InputStreamReader(in, "utf-8"));
properties = new Properties();
try {
properties.load(bf);
} catch (IOException e) {
throw e;
}
} else {
logger.error(FILE_NAME+ "属性文件未能在classpath指定的目录下 "+ AppConfig.class.getClassLoader().getResource("").getPath() + " 找到!");
return;
}
loadProperties(properties);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (null != in) {
try {
in.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
} /**
* 根据传入的properties对象设置配置参数
*
* @param pro
*/
private void loadProperties(Properties pro) {
logger.info("开始从属性文件中加载配置项");
String value = null;
value = pro.getProperty("xwcgGatewayUrl");
if (!StringUtils.isEmpty(value)) {
this.xwcgGatewayUrl = value.trim();
}
value = pro.getProperty("xwcgServiceUrl");
if (!StringUtils.isEmpty(value)) {
this.xwcgServiceUrl = value.trim();
}
value = pro.getProperty("xwcgDownloadUrl");
if (!StringUtils.isEmpty(value)) {
this.xwcgDownloadUrl = value.trim();
}
value = pro.getProperty("platformNo");
if (!StringUtils.isEmpty(value)) {
this.platformNo = value.trim();
}
value = pro.getProperty("keySerial");
if (!StringUtils.isEmpty(value)) {
this.keySerial = value.trim();
}
value = pro.getProperty("privateKey");
if (!StringUtils.isEmpty(value)) {
this.privateKey = value.trim();
}
value = pro.getProperty("publicKey");
if (!StringUtils.isEmpty(value)) {
this.publicKey = value.trim();
}
value = pro.getProperty("lmPrivateKey");
if (!StringUtils.isEmpty(value)) {
this.lmPrivateKey = value.trim();
}
value = pro.getProperty("lmPublicKey");
if (!StringUtils.isEmpty(value)) {
this.lmPublicKey = value.trim();
}
value = pro.getProperty("connectionTimeout");
if (!StringUtils.isEmpty(value)) {
this.connectionTimeout = Integer.valueOf(value.trim());
}
value = pro.getProperty("readTimeout");
if (!StringUtils.isEmpty(value)) {
this.readTimeout = Integer.valueOf(value.trim());
} } public String getXwcgGatewayUrl() {
return xwcgGatewayUrl;
} public void setXwcgGatewayUrl(String xwcgGatewayUrl) {
this.xwcgGatewayUrl = xwcgGatewayUrl;
} public String getXwcgServiceUrl() {
return xwcgServiceUrl;
} public void setXwcgServiceUrl(String xwcgServiceUrl) {
this.xwcgServiceUrl = xwcgServiceUrl;
} public String getXwcgDownloadUrl() {
return xwcgDownloadUrl;
} public void setXwcgDownloadUrl(String xwcgDownloadUrl) {
this.xwcgDownloadUrl = xwcgDownloadUrl;
} public String getPlatformNo() {
return platformNo;
} public void setPlatformNo(String platformNo) {
this.platformNo = platformNo;
} public String getKeySerial() {
return keySerial;
} public void setKeySerial(String keySerial) {
this.keySerial = keySerial;
} public String getPrivateKey() {
return privateKey;
} public void setPrivateKey(String privateKey) {
this.privateKey = privateKey;
} public int getConnectionTimeout() {
return connectionTimeout;
} public void setConnectionTimeout(int connectionTimeout) {
this.connectionTimeout = connectionTimeout;
} public int getReadTimeout() {
return readTimeout;
} public void setReadTimeout(int readTimeout) {
this.readTimeout = readTimeout;
} public String getPublicKey() {
return publicKey;
} public void setPublicKey(String publicKey) {
this.publicKey = publicKey;
} public String getLmPrivateKey() {
return lmPrivateKey;
} public void setLmPrivateKey(String lmPrivateKey) {
this.lmPrivateKey = lmPrivateKey;
} public String getLmPublicKey() {
return lmPublicKey;
} public void setLmPublicKey(String lmPublicKey) {
this.lmPublicKey = lmPublicKey;
}
}

java工具类获取properties文件的配置的更多相关文章

  1. Property工具类,Properties文件工具类,PropertiesUtils工具类

    Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...

  2. springboot获取properties文件的配置内容(转载)

    1.使用@Value注解读取读取properties配置文件时,默认读取的是application.properties. application.properties: demo.name=Name ...

  3. java工具类 获取包下所有类

    extends:http://blog.csdn.net/jdzms23/article/details/17550119 package com.threeti.util; import java. ...

  4. 文件读取工具类读取properties文件

    1.创建工具类 import java.io.IOException; import java.util.Properties; /** * * 类名称:PropertiesUtil * 类描述: 文 ...

  5. JAVA工具类获取HttpServletRequest、HttpServletResponse 对象

    添加依赖 <!-- Spring Web --> <dependency> <groupId>org.springframework</groupId> ...

  6. 【java工具类】删除文件及目录

    FileUtil.java /** * 删除文件及目录 * @param file; */ public static boolean delFile(File file) { if (!file.e ...

  7. 【java工具类】下载文件

    FileUtil.java /** * 下载文件 * @param file; * @param response */ public static void downloadFile(File fi ...

  8. java读取properties文件的配置信息

    项目开发中,我们一般来向 application.properties 文件中放一些全局配置变量,以便程序中读取使用,本篇内容来演示从properties配置文件中读取键值. 当然,我们不一定写入 a ...

  9. Java工具类——通过配置XML验证Map

    Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...

随机推荐

  1. HDU1267 下沙的沙子有几粒? 基础DP

    题目链接 题意:给定m个H和n个D(1<=n,m<=20),问这些字母构成的序列中,对于任意位置,从左开始数H的累积个数总是不比D的累计数少的排列有多少种. 题解:二维DP,画一个正方形, ...

  2. HDU2819(二分图匹配,记录过程)

    Swap Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submis ...

  3. PL/SQL 01 代码编写规则

    1.标识符命名规则当在 PL/SQL 中使用标识符定义变量.常量时,标识符名称必须以字符开始,并且长度不能超过 30 个字符.另外,为了提高程序的可读性,Oracle 建议用户按照以下规则定义各种标识 ...

  4. IE6 下的HTML5兼容问题

    下面列举IE6中10个不得不注意的问题: 1. 使用 DOCTYPE你需要在HTML页面的最顶部加上DOCTYPE类型,当然, strict版是值得推荐的,例如: <!DOCTYPE HTML ...

  5. python高阶函数,map,filter,reduce,ord,以及lambda表达式

    为什么我突然扯出这么几个函数,是因为我今天在看流畅的python这本书的时候,里面有一部分内容看的有点懵逼. >>> symbols = '$¢£¥€¤' >>> ...

  6. 使用bottle进行web开发(4):HTTPError

    from bottle import error @error(404) def error404(error): return 'Nothing here, sorry' 上述代码,是对404的定义 ...

  7. 使用 gulp 编译 Sass

    无论是 node-sass 还是 ruby-sass 使用 npm 安装都非常的慢,甚至会装不上.及其不利于团队协作.建议使用 less 作为 css 预处理器. 如果因为 less 不支持自定义函数 ...

  8. Redis 源码走读(一)事件驱动机制与命令处理

    eventloop 从 server.c 的 main 方法看起 int main(int argc, char **argv) { ....... aeSetBeforeSleepProc(serv ...

  9. (十)while和until循环

    (1)while循环 语法:当条件测试成立(真),执行循环体 while 条件测试 do 循环体 done 1)while批量创建用户1 从user.txt读取到的行数据赋予给变量user值 #!/b ...

  10. BZOJ 3223: Tyvj 1729 文艺平衡树-Splay树(区间翻转)模板题

    3223: Tyvj 1729 文艺平衡树 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 6881  Solved: 4213[Submit][Sta ...