【java】 一个读取配置文件的类

版权声明:本文为博主原创文章,转载请标明来源http://blog.csdn.net/srk950606 宋荣凯的博客 https://blog.csdn.net/srk950606/article/details/81274443
/**
 * <p>Title:InitConfig.java</p>
 * <p>Description:</p>
 * @author songrongkai
 * @date  2018年7月29日
 * @version 1.0
 */
package com.candyshop.utils.util;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;

import org.apache.commons.lang.StringUtils;

/**
 * <p>
 * Title: InitConfig
 * </p>
 * <p>
 * Description:
 * </p>
 * 
 * @author sonrongkai
 * @date 2018年7月29日
 */
public class InitConfig extends HttpServlet {
	private static java.util.Properties prop;
	private static final org.slf4j.Logger LOGGER = org.slf4j.LoggerFactory
			.getLogger(InitConfig.class);

	@Override
	public void init(ServletConfig config) throws ServletException {
		String configFIleString = config.getInitParameter("configFIle");
		prop = new java.util.Properties();
		FileInputStream in = null;
		InputStreamReader isr = null;
		if (StringUtils.isNotBlank(configFIleString)) {
			try {
				in = new FileInputStream(configFIleString);
				isr = new InputStreamReader(in, "UTF-8");
				prop.load(isr);
				;
			} catch (Exception e) {
				LOGGER.error(e.getLocalizedMessage(), e);
			} finally {
				if (null != isr) {
					try {
						isr.close();
					} catch (IOException e2) {
						LOGGER.error("IO异常" + e2.getMessage());
					}

				}
				if (null != in) {
					try {
						in.close();
					} catch (IOException e2) {
						LOGGER.error("IO异常" + e2.getMessage());
					}

				}
			}

		}
	}

	public static String getConfig(String key) {
		return prop.getProperty(key);
	}

}

项目里是直接用InitConfig.getConfig 获取配置文件里的信息的。 我找了半天也没有找到到底是在哪初始化的, 笑哭~ 用的是民生银行自己的内部框架 Firefly 和tesla

猜你喜欢

转载自blog.csdn.net/srk950606/article/details/81274443