分别介绍两种不同的 PropertiesUtils 工具类

1.优点:不依懒于spring容器 ,缺点:只能读取单个properties文件

import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Date;
import java.util.Properties;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.alibaba.fastjson.util.TypeUtils;

/**
 * java读取配置文件
 * 
 * @author Neo 2017-5-12
 * @version 1.1
 * 依赖于:fastjson-1.2.7.jar
 *
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class PropertiesUtils {

	private static Logger logger = LoggerFactory.getLogger(PropertiesUtils.class);

	private static Properties properties;

	private static final String PROPERTIES_EGIS_FILE_NAME = "application/config.properties";

	static {
		properties = new Properties();
		InputStream scmsStream = null;
		try {
			scmsStream = PropertiesUtils.class.getClassLoader().getResourceAsStream(PROPERTIES_EGIS_FILE_NAME);
			properties.load(scmsStream);
			logger.info("PropertiesUtils", "staitc init prop", properties.toString());

		} catch (Exception e) {
		} finally {
			try {
				if (scmsStream != null) {
					scmsStream.close();
				}
			} catch (Exception e) {

			}
		}
	}

	public static String getProperty(String key) {
		String result = properties.getProperty(key);
		return result;
	}

	public static String getProperty(String key, String defaultValue) {
		String result = properties.getProperty(key, defaultValue);
		return result;
	}

	public static String getProperties(String propertiesName, String key) {
		Properties props = new Properties();
		InputStream inputStream = null;
		try {
			inputStream = PropertiesUtils.class.getClassLoader().getResourceAsStream(propertiesName);
			props.load(inputStream);
		} catch (IOException e) {
		} finally {
			try {
				if (inputStream != null) {
					inputStream.close();
				}
			} catch (Exception e) {

			}
		}

		return props.getProperty(key);
	}

	public static Object getObject(String key, Class clazz) {
		Object obj = getProperty(key);
		return TypeUtils.castToJavaBean(obj, clazz);
	}

	public static Boolean getBoolean(String key) {
		Object value = getProperty(key);
		if (value == null)
			return null;
		else
			return TypeUtils.castToBoolean(value);
	}

	public static byte[] getBytes(String key) {
		Object value = getProperty(key);
		if (value == null)
			return null;
		else
			return TypeUtils.castToBytes(value);
	}

	public static boolean getBooleanValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return false;
		else
			return TypeUtils.castToBoolean(value).booleanValue();
	}

	public static Byte getByte(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToByte(value);
	}

	public static byte getByteValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return 0;
		else
			return TypeUtils.castToByte(value).byteValue();
	}

	public static Short getShort(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToShort(value);
	}

	public static short getShortValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return 0;
		else
			return TypeUtils.castToShort(value).shortValue();
	}

	public static Integer getInteger(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToInt(value);
	}

	public static int getIntValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return 0;
		else
			return TypeUtils.castToInt(value).intValue();
	}

	public static Long getLong(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToLong(value);
	}

	public static long getLongValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return 0L;
		else
			return TypeUtils.castToLong(value).longValue();
	}

	public static Float getFloat(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToFloat(value);
	}

	public static float getFloatValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return 0.0F;
		else
			return TypeUtils.castToFloat(value).floatValue();
	}

	public static Double getDouble(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToDouble(value);
	}

	public static double getDoubleValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return 0.0D;
		else
			return TypeUtils.castToDouble(value).doubleValue();
	}

	public static BigDecimal getBigDecimal(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToBigDecimal(value);
	}

	public static BigInteger getBigInteger(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToBigInteger(value);
	}

	public static String getString(String key) {
		Object value = getProperty(key);
		if (value == null)
			return null;
		else
			return value.toString();
	}

	public static Date getDate(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToDate(value);
	}

	public static java.sql.Date getSqlDate(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToSqlDate(value);
	}

	public static Timestamp getTimestamp(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToTimestamp(value);
	}

	public static void main(String[] args) {
		if(getBooleanValue("isNeedLogin"))
			System.out.println("aaa");
	}
}

2.优点:可以读取多个properties的内容,缺点:依赖于spring容器

import java.math.BigDecimal;
import java.math.BigInteger;
import java.sql.Timestamp;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.Properties;

import org.springframework.beans.BeansException;
import org.springframework.beans.factory.config.ConfigurableListableBeanFactory;
import org.springframework.beans.factory.config.PropertyPlaceholderConfigurer;

import com.alibaba.fastjson.util.TypeUtils;

/**
 * java读取配置文件
 * 
 * @author Neo 2017-5-12
 * @version 1.2
 * 依赖于:fastjson-1.2.7.jar
 * 
 *          <pre>
 *  spring-bean.xml 中做如下配置<br/>
 * 	<bean id="PropertiesUtils" class="com.shebao.sbt.fmis.common.util.PropertiesUtils">
 *  	<property name="locations">
 *  		<list>
 *  			<value>classpath:application/*.properties</value>
 *  		</list>
 *  	</property>
 *  </bean>
 *          </pre>
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public class PropertiesUtils extends PropertyPlaceholderConfigurer {

	private static Map<String, Object> ctxPropertiesMap;

	/**
	 * 重写processProperties方法
	 */
	@Override
	protected void processProperties(ConfigurableListableBeanFactory beanFactoryToProcess, Properties props)
			throws BeansException {
		super.processProperties(beanFactoryToProcess, props);
		ctxPropertiesMap = new HashMap<String, Object>();
		for (Object key : props.keySet()) {
			String keyStr = key.toString();
			String value = props.getProperty(keyStr);
			ctxPropertiesMap.put(keyStr, value);
		}
	}

	public static String getProperty(String key) {
		return TypeUtils.castToString(ctxPropertiesMap.get(key));
	}

	public static Object getObject(String key, Class clazz) {
		Object obj = getProperty(key);
		return TypeUtils.castToJavaBean(obj, clazz);
	}

	public static Boolean getBoolean(String key) {
		Object value = getProperty(key);
		if (value == null)
			return null;
		else
			return TypeUtils.castToBoolean(value);
	}

	public static byte[] getBytes(String key) {
		Object value = getProperty(key);
		if (value == null)
			return null;
		else
			return TypeUtils.castToBytes(value);
	}

	public static boolean getBooleanValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return false;
		else
			return TypeUtils.castToBoolean(value).booleanValue();
	}

	public static Byte getByte(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToByte(value);
	}

	public static byte getByteValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return 0;
		else
			return TypeUtils.castToByte(value).byteValue();
	}

	public static Short getShort(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToShort(value);
	}

	public static short getShortValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return 0;
		else
			return TypeUtils.castToShort(value).shortValue();
	}

	public static Integer getInteger(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToInt(value);
	}

	public static int getIntValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return 0;
		else
			return TypeUtils.castToInt(value).intValue();
	}

	public static Long getLong(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToLong(value);
	}

	public static long getLongValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return 0L;
		else
			return TypeUtils.castToLong(value).longValue();
	}

	public static Float getFloat(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToFloat(value);
	}

	public static float getFloatValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return 0.0F;
		else
			return TypeUtils.castToFloat(value).floatValue();
	}

	public static Double getDouble(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToDouble(value);
	}

	public static double getDoubleValue(String key) {
		Object value = getProperty(key);
		if (value == null)
			return 0.0D;
		else
			return TypeUtils.castToDouble(value).doubleValue();
	}

	public static BigDecimal getBigDecimal(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToBigDecimal(value);
	}

	public static BigInteger getBigInteger(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToBigInteger(value);
	}

	public static String getString(String key) {
		Object value = getProperty(key);
		if (value == null)
			return null;
		else
			return value.toString();
	}

	public static Date getDate(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToDate(value);
	}

	public static java.sql.Date getSqlDate(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToSqlDate(value);
	}

	public static Timestamp getTimestamp(String key) {
		Object value = getProperty(key);
		return TypeUtils.castToTimestamp(value);
	}

}


  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值