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项目中,接收前端过来的参数时通常是使用我们的实体类进行接收的.但是呢,我们不能去决定已经搭建好的框架是怎么样的,在我接触的框架中有一种 ...
随机推荐
- Hadoop之计数器与自定义计数器及Combiner的使用
1,计数器: 显示的计数器中分为四个组,分别为:File Output Format Counters.FileSystemCounters.File Input Format Counters和Ma ...
- DotNETCore 学习笔记 日志
Logging --------------------------------------------------------------------------------------- Impl ...
- I2C和SPI总线对比【转】
转自:http://blog.csdn.net/skyflying2012/article/details/8237881/ 最近2周一直在调试IIC和SPI总线设备,这里记录一下2种总线,以备后忘. ...
- vue指令v-bind
v-bind用于绑定 html 属性,通常会将v-bind缩写(如"v-bind:class"可缩写成":class"): v-bind除了可以绑定字符串类型变 ...
- python中deque类详解
最近在pythonTip做题的时候,遇到了deque类,以前对其不太了解,现在特此总结一下 deque类是python标准库collections模块中的一项,它提供了两端都可以操作的序列,这意味着, ...
- 深入解析当下大热的前后端分离组件django-rest_framework系列一
前言 Nodejs的逐渐成熟和日趋稳定,使得越来越多的公司开始尝试使用Nodejs来练一下手,尝一尝鲜.在传统的web应用开发中,大多数的程序员会将浏览器作为前后端的分界线.将浏览器中为用户进行页面展 ...
- javascript实现网页返回顶部功能
在浏览网页时,我们一般是拖动滚动条向下滑动,浏览下面的内容,当页面超过单页时,右下角会出现一个回到顶部的图标,有些网站这个图标一直显示在右下角的. 有些网站使用锚链接来实现页面内容的跳转,但这种效果的 ...
- hdu 5170(数学)
GTY's math problem Time Limit: 1000/1000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Other ...
- 使用 mybatis和oracle 数据库出现的问题
mybatis 官网教程 http://www.mybatis.org/mybatis-3/zh/dynamic-sql.html 出现 Could not set parameters for ma ...
- thinkphp5最美跳转页面
声明下:此教程来自TP官网,如果需要看原文,请点击一下链接 http://www.thinkphp.cn/code/3437.html 先给大家看下效果: 直接撸代码: 第一步:为了增加对移动设备 ...