Configuration

package edu.fzu.ir.util;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.util.Properties;

import org.apache.log4j.Logger;

/**
 * 读取配置文件的工具类
 * 提供了静态读取配置文件的方法
 * 改配置文件是放在每个worker节点??
 * worker启动时根据配置文件路径读取该配置文件
 * @author hasee
 *
 */
public class Configuration {
    private static Logger logger = Logger.getLogger(Configuration.class);
    private static Properties properties;
    private Configuration() {}

    /**
     * 通过配置文件的路径获取相关的配置,存储在properties中,worker每次启动时调用该方法
     * @param confPath 配置文件的路??
     */
    public static void loadConfiguration(String confPath) {
        //判断properties是否是空,如果不为空表示已经加载过配置文件,不需要再加载
        if(properties==null) {
            //利用InputStreamReader指定利用utf-8编码读取properties文件
            //这样是为了解决配置文件中的中文乱码问??
            InputStream inputStream = null;
            InputStreamReader isr = null;
            try {
                inputStream = new FileInputStream(new File(confPath));
                isr = new InputStreamReader(inputStream, "UTF-8");
            } catch (FileNotFoundException e) {
                //文件未找到时,输出错误日志,并停止系??
                logger.error(e);
                System.exit(0);
            } catch (UnsupportedEncodingException e) {
                logger.error(e);
                System.exit(0);
            }
            properties = new Properties();
            try {
                //加载并存储到properties
                logger.info("load configuration from : "+ confPath);
                properties.load(isr);
                logger.info("load succeed!");
            } catch (IOException e) {
                logger.error(e);
            } finally {
                //关闭资源
                try {
                    if(inputStream != null) {
                        inputStream.close();
                    }
                    if(isr != null) {
                        isr.close();
                    }
                } catch (IOException e) {
                    logger.error(e);
                }
            }
        }
    }

    /**
     * 获取配置文件中某????配置??若不存在返回null
     * @param key 配置项的key??
     * @return
     */
    public static String getProperties(String key) {
        String s = null;
        if(properties != null){
            s = properties.getProperty(key);
        }
        return s;
    }
}
时间: 2024-10-12 15:15:40

Configuration的相关文章

ubuntu开机出现waiting for network configuration

ubuntu启动时,出现waiting for network configuration,waiting up to 60 more seconds for network configuration等,进入桌面后网络图标也不见了 解决方法,首先在 /etc/network/interfaces 文件里面无关的都删去,留下lo这个 然后再到/etc/init/failsafe.conf文件里将sleep59改成5或10,sleep50意思是等待59秒

出现unmapped spring configuration files found

intell idea启动出现unmapped spring configuration files found提示. 把spring里面的内容都打勾.

Check if a configuration profile is installed on iOS

Configuration profiles can be downloaded to an iOS device through Safari to configure the device in a certain way. You can do things like force the user to set a passcode or set restrictions like not allowing them to install apps or not allowing acce

Apache Commons Configuration之一简介

1    简介 Commons Configuration软件类库提供通用配置接口,使Java应用程序从多种源读取配置文件.Commons Configuration提供简单类型访问和通过以下代码演示的多义配置参数: Double double = config.getDouble("number"); Integer integer = config.getInteger("number"); 配置参数可以从以下源加载: Properties文件 XML文档 Pr

System Center 2012 R2 CM系列之配置configuration manager防火墙设置

为了能够正常推送Configuration Manager 2012 R2客户端,必须添加防火墙例外设置,主要包括以下两个:A:文件和打印共享:B: Windows Management Instrumentation (WMI) 1. 打开并登录"BJ-DC-01"服务器,点击工具,打开组策略管理器 2. 右键相关域,点击创建"在这个域中创建GPO并在此处链接" 3. 新建GPO对话框中输入相应的GPO名称 4. 右键新创建的PGO,点击编辑,并打开组策略管理编辑

MyBatis框架中Mapper映射配置的使用及原理解析(三) 配置篇 Configuration

从上文<MyBatis框架中Mapper映射配置的使用及原理解析(二) 配置篇 SqlSessionFactoryBuilder,XMLConfigBuilder> 我们知道XMLConfigBuilder调用parse()方法解析Mybatis配置文件,生成Configuration对象. Configuration类主要是用来存储对Mybatis的配置文件及mapper文件解析后的数据,Configuration对象会贯穿整个Mybatis的执行流程,为Mybatis的执行过程提供必要的配

setup界面的network configuration 进不去的原因

setup界面的network configuration 进不去的原因 这个问题在百度上搜了好久都没搜到能解决的答案,最后还是自己琢磨出来的. 目前我遇进不去的原因是,因为在刚装好系统(装的是最小化的)后直接暴力装的system-config-network-tui,导致缺少依赖包,造成了点不进去. 解决办法: rpm-qa | grep 'system-config-network-tui' 查看所有安装包并过滤出网络安装包            rpm-e system-config-ne

Spring - Java Based Configuration

So far you have seen how we configure Spring beans using XML configuration file. If you are comfortable with XML configuration, then it is really not required to learn how to proceed with Java-based configuration as you are going to achieve the same

Spring - Annotation Based Configuration

Starting from Spring 2.5 it became possible to configure the dependency injection using annotations. So instead of using XML to describe a bean wiring, you can move the bean configuration into the component class itself by using annotations on the re

UDEV SCSI Rules Configuration for ASM in Oracle Linux 5 and 6

UDEV SCSI Rules Configuration for ASM in Oracle Linux 5 and 6 For Oracle Automatic Storage Manager (ASM) to use disks, it needs to be able to identify the devices consistently and for them to have the correct ownership and permissions. In Linux you c