PropertiesUtil.java
//
//
// Generated by StarUML(tm) Java Add-In
//
// @ Project : Untitled
// @ File Name : ServiceInitListener.java
// @ Date : 2013/1/16
// @ Author : czp
// @ Title : 配置文件加载工具
// @ Description : 对config.properties进行读取,由ServiceInitListener进行初始化
//
//
package com.fjxhx.business.system.util;
import java.io.BufferedInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.log4j.Logger;
public class PropertiesUtil {
// 只做读取,如添加修改须加入线程安全
private static Logger logger = Logger.getLogger(PropertiesUtil.class);
private static Properties configProperty;
private static Properties errorProperties;
public static void createProperties(String path) {
try {
InputStream in = new BufferedInputStream(new FileInputStream(path));
configProperty = new Properties();
configProperty.load(in);
} catch (IOException e) {
logger.error(e);
}
}
public static void createErrorProperties(String path) {
try {
InputStream in = new BufferedInputStream(new FileInputStream(path));
errorProperties = new Properties();
errorProperties.load(in);
} catch (IOException e) {
logger.error(e);
}
}
// 中文必须要要用(\u6211\u662F\u7F16\u7801)编码
public static String getConfigProperty(String key) {
String value = getConfigProperty().getProperty(key);
if (value != null) {
return value;
}
return null;
}
public static String getErrorProperty(String key) {
String value = getErrorProperties().getProperty(key);
if (value != null) {
return value;
}
return null;
}
public static Properties getErrorProperties() {
return errorProperties;
}
public static void setErrorProperties(Properties errorProperties) {
PropertiesUtil.errorProperties = errorProperties;
}
public static Properties getConfigProperty() {
return configProperty;
}
public static String objectReplace(String value, Object object) {
if (value == null) {
return null;
}
String regex = "\\$([^\\$]+)\\$";// 匹配所有 $hello$ 这类标记
Pattern p = Pattern.compile(regex);
Matcher m = p.matcher(value);
while (m.find()) {
String pre = m.group(1);
String getFun = "get" + pre.substring(0, 1).toUpperCase()
+ pre.substring(1);
Class klass = object.getClass();
try {
Method method = klass.getMethod(getFun);
Object obj = method.invoke(object);
if (obj == null) {
obj = "";
}
if (Date.class.isAssignableFrom(obj.getClass())) {
obj = PropertiesUtil.parseDate((Date) obj);
}
value = value.replaceAll("\\$" + pre + "\\$", obj.toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
return value;
}
private static String dateFormat = "yyyy-MM-dd HH:mm:ss";
public static Properties toProperties(Object bean) {
if (bean == null)
return null;
Properties pro = new Properties();
Class klass = bean.getClass();
Method[] methods = klass.getMethods();
Method method;
String name, key;
Object obj = null;
for (int i = 0; i < methods.length; i += 1) {
try {
method = methods[i];
name = method.getName();
key = "";
if (name.startsWith("get")) {
key = name.substring(3);
} else if (name.startsWith("is")) {
key = name.substring(2);
}
if (key.length() > 0 && !key.equals("Class")
&& Character.isUpperCase(key.charAt(0))
&& method.getParameterTypes().length == 0) {
key = key.substring(0, 1).toLowerCase() + key.substring(1);
obj = method.invoke(bean, (Object[]) null);
// 仅处理 String, Number, Boolean, Date 这几种类型
if (obj == null) {
} else if (Date.class.isAssignableFrom(obj.getClass())) {
pro.setProperty(key, parseDate((Date) obj, dateFormat));
} else if (obj instanceof Object) {
pro.setProperty(key, obj.toString());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
return pro;
}
/** 日期到字符串 */
public static String parseDate(Date date, String pattern) {
if (date == null) {
return null;
}
try {
SimpleDateFormat sdf = new SimpleDateFormat(pattern);
return sdf.format(date);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
public static String parseDate(Date date) {
if (date == null) {
return null;
}
try {
SimpleDateFormat sdf = new SimpleDateFormat(dateFormat);
return sdf.format(date);
} catch (Exception ex) {
ex.printStackTrace();
return null;
}
}
}
读取:
private final static String ORDER_SUC = PropertiesUtil.getConfigProperty().getProperty("weixin.order.templateId");