Java加载配置文件工具类

1)加载全局配置文件类

package com.magnus.core.servlet;

import java.io.File;

import java.io.IOException;

import java.io.PrintWriter;

import javax.servlet.ServletException;

import javax.servlet.http.HttpServlet;

import javax.servlet.http.HttpServletRequest;

import javax.servlet.http.HttpServletResponse;

扫描二维码关注公众号,回复: 524307 查看本文章

import org.apache.log4j.Logger;

import org.springframework.web.context.WebApplicationContext;

import org.springframework.web.context.support.WebApplicationContextUtils;

import org.springside.modules.utils.SystemGlobals;

/**

 * 主要用于加载全局配置文件的类

 * 

 * 

 */

public class ConfigServlet extends HttpServlet {

private static final long serialVersionUID = 1L;

private final static Logger log = Logger.getLogger(ConfigServlet.class);

/**

* Constructor of the object.

*/

public ConfigServlet() {

super();

}

/**

* Destruction of the servlet. <br>

*/

public void destroy() {

super.destroy(); // Just puts "destroy" string in log

// Put your code here

}

/**

* Initialization of the servlet. <br>

* @throws ServletException

*             if an error occure

*/

public void init() throws ServletException {

try {

// ConfigConstant.initConfigConstant(getServletContext().getRealPath("/"));

load();

log.info("Init config.......ok");

log.info(SystemGlobals.dump());

} catch (Exception e) {

log.error(e);

throw new ServletException("Init config Fail:" + e.getMessage());

}

String filePath = SystemGlobals.getValue("filepath");

// 其他必要操作。。。

}

public void doPost(HttpServletRequest request, HttpServletResponse response) {

try {

load();

log.info("Reload config.......ok");

log.info(SystemGlobals.dump());

// 清空缓存

WebApplicationContext webApplicationContext = WebApplicationContextUtils

.getRequiredWebApplicationContext(getServletContext());

PrintWriter out = response.getWriter();

out.print("{success:true}");

out.flush();

out.close();

} catch (Exception e) {

log.error("reload config Fail:" + e.getMessage());

}

}

public void doGet(HttpServletRequest request, HttpServletResponse response) {

doPost(request, response);

}

private void load() throws IOException {

String configFile = this.getServletConfig().getInitParameter("configFile");

SystemGlobals.loadConfig(this.getServletContext().getRealPath(configFile));

}

}

2) 提供全局配置文件访问入口

package org.springside.modules.utils;

import java.io.FileInputStream;

import java.io.IOException;

import java.util.Properties;

import org.apache.log4j.Logger;

/**

 * 单件,提供全局配置文件访问入口

 */

public class SystemGlobals {

    private final static Logger log = Logger.getLogger(SystemGlobals.class);

private static SystemGlobals globals = new SystemGlobals(); 

private Properties config = new Properties();

private SystemGlobals(){}

/**

* 静态加载配置文件

* @param appPath 文件路径

* @throws IOException

*/

public static void loadConfig(String appPath) throws IOException

{

globals = new SystemGlobals();

FileInputStream input = new FileInputStream(appPath);

globals.config.load(input);

input.close();

}

/**

* 取得与key值相对应的字符串value

* @param key

* @return 字符串value

*/

public static String getValue(String key){

return globals.config.getProperty(key);

}

/**

* 取得与key值相对应的int类型的value

* @param key

* @return int类型的值

*/

public static int getIntValue(String key){

return Integer.parseInt(globals.config.getProperty(key).trim());

}

public static String dump(){

return globals.config.toString();

}

}

猜你喜欢

转载自ljm653467.iteye.com/blog/2163384