java工具类获取properties文件的配置
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文件的配置的更多相关文章
- Property工具类,Properties文件工具类,PropertiesUtils工具类
Property工具类,Properties文件工具类,PropertiesUtils工具类 >>>>>>>>>>>>>& ...
- springboot获取properties文件的配置内容(转载)
1.使用@Value注解读取读取properties配置文件时,默认读取的是application.properties. application.properties: demo.name=Name ...
- java工具类 获取包下所有类
extends:http://blog.csdn.net/jdzms23/article/details/17550119 package com.threeti.util; import java. ...
- 文件读取工具类读取properties文件
1.创建工具类 import java.io.IOException; import java.util.Properties; /** * * 类名称:PropertiesUtil * 类描述: 文 ...
- JAVA工具类获取HttpServletRequest、HttpServletResponse 对象
添加依赖 <!-- Spring Web --> <dependency> <groupId>org.springframework</groupId> ...
- 【java工具类】删除文件及目录
FileUtil.java /** * 删除文件及目录 * @param file; */ public static boolean delFile(File file) { if (!file.e ...
- 【java工具类】下载文件
FileUtil.java /** * 下载文件 * @param file; * @param response */ public static void downloadFile(File fi ...
- java读取properties文件的配置信息
项目开发中,我们一般来向 application.properties 文件中放一些全局配置变量,以便程序中读取使用,本篇内容来演示从properties配置文件中读取键值. 当然,我们不一定写入 a ...
- Java工具类——通过配置XML验证Map
Java工具类--通过配置XML验证Map 背景 在JavaWeb项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...
随机推荐
- linux 系统函数之 (dirname, basename)【转】
转自:http://blog.csdn.net/peter_cloud/article/details/9308333 版权声明:本文为博主原创文章,未经博主允许不得转载. 除非你的原件考虑跨平台. ...
- html怎样让表格里面的内容居中
html怎样让表格里面的内容居中 text-align:center; 在表格td中,有两个属性控制居中显示 align——表示左右居中——left,center,right valign——控制上下 ...
- 微信小程序保存图片的方法
1.xhtml代码 长按保存: <view class="img" catchlongpress='baocun'></view> 2.Js代码 baocu ...
- AC日记——摆花
思路: 矩阵加速dp: 代码: #include <cstdio> #include <cstring> #include <iostream> #include ...
- C++的Public.lib(Public.dll) : fatal error LNK1112: module machine type 'X86' conflicts with target machine type 'x64'
今天开始编译网游服务器,找前辈借来批处理文件,版本控制上拿下代码,库等一系列资源,尼玛啊,编译出错: Public.lib(Public.dll) : fatal error LNK1112: mod ...
- 【转】docker之Dockerfile实践
转自:https://www.cnblogs.com/jsonhc/p/7767669.html 上一篇介绍了Dockerfile中使用的指令,现在开始进行指令实践 先查看下本地的镜像,选一个作为ba ...
- JS中对数组的操作方法
不断加入中.... 一.数组的增删 1.push():从后面追加 pop():从后面删除一个. 二.数组与字符串的转换 split():用分隔符生成数组 join():将数组用分隔符连为字符串. 三. ...
- 优化MySQL插入方法的五个妙招
以下是涉及到插入表格的查询的5种改进方法: 1)使用LOAD DATA INFILE从文本下载数据这将比使用插入语句快20倍. 2)使用带有多个VALUES列表的INSERT语句一次插入几行这将比使用 ...
- (14)python 文件和流
打开文件 f=open('C:\Temp.txt') 读取数据 f.read(); 关闭文件 f.close();#关闭后将无法再读取 打开文件的方式 不写模式,默认是只读模式 1.r 打开只读文件, ...
- CF981C Useful Decomposition【树/思维】
[链接]:CF [题意]:给定一棵树,要求拆成若干条简单路径,并且这些路径都经过一个公共节点.给出任意一个解决方案,如不存在输出No. [分析]: 因为是一棵树, 所以如果要求任意两条路线至少有一个公 ...