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. PHP文件操作函数一

    <?php/*Created on 2013-6-26*///判断文件的类型echo filetype("array.php")."<br />&quo ...

  2. bzoj4430 [Nwerc2015]Guessing Camels赌骆驼

    传送门:http://www.lydsy.com/JudgeOnline/problem.php?id=4430 [题解] 把每只骆驼在第一个人.第二个人.第三个人的位置找出来,然后做三维偏序即可. ...

  3. 在ubuntu 上面安装ubuntu touch 模拟器

    Canonical 公司已经发布了一个运行着Unity8和Mir的Ubuntu Touch模拟器.虽然有一些bug,例如在64位的系统上会使系统崩溃,但我们相信这些都会被一 一修复,这篇文章将教大家如 ...

  4. 【Sqlite3】SQLITE3使用总结(转)

    原文转自 https://www.cnblogs.com/wenxp2006/archive/2012/06/04/2535169.html SQL语句操作 介绍如何用sqlite 执行标准 sql  ...

  5. C++11 自动释放锁(转)

    原文转自 https://blog.csdn.net/lmb1612977696/article/details/77712170 c++11加入了很多新的特性,值得我们去探索. 先看一个例子:普通的 ...

  6. Linux内核完全剖析基于0.12内核

    控制寄存器(CR0,CR1,CR2,CR3)用于控制和确定处理器的操作模式以及当前执行任务的特性.CR0中含有控制处理器操作模式和状态的系统控制标志,CR1保留不用,CR2含有导致页错误的线性地址,C ...

  7. YSlow安装兼容的环境版本

    YSlow:网站性能评分工具 1.安装 Firefox 282.安装 Firebug1.2.83.安装 YSlow3.1.8.1

  8. IOS UITableViewUITableView小技巧--实现cell向左滑动删除,编辑等功能

    - (BOOL)tableView:(UITableView *)tableView canEditRowAtIndexPath:(NSIndexPath *)indexPath { return Y ...

  9. 错误: 在类 Main 中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args) 否则 JavaFX 应用程序类必须扩展javafx.application.Application

    错误: 在类 Main 中找不到 main 方法, 请将 main 方法定义为: public static void main(String[] args)否则 JavaFX 应用程序类必须扩展ja ...

  10. JSONObject依赖包

    commons-lang.jar commons-beanutils.jar commons-collections.jar commons-logging.jar ezmorph.jar json- ...