单例模式获取配置文件.properties

package com.pds.WebConfig;

/**
 * <p>Title: </p>
 *
 * <p>Description: </p>
 *
 * <p>Copyright: Copyright (c) 2008</p>
 *
 * <p>Company: </p>
 *
 * @author not attributable
 * @version 1.0
 */
import org.apache.commons.io.IOUtils;
import org.springframework.core.io.Resource;
import org.springframework.core.io.support.PathMatchingResourcePatternResolver;
import org.springframework.core.io.support.ResourcePatternResolver;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.Properties;
public class ConfigManager {
    public static void main(String[] args){
         String a= ConfigManager.getParameter("tokenUrl");
         System.out.print("aaa=="+a);
    }
    public static final String PROPERTY_FILE = "zhiyuanxml/zhiyuanconfig.properties";
    private static Properties props;
    private static ConfigManager instance;
    static{
        getInstance();
    }
    /**
     * 私有的构造函数
     */
    private ConfigManager() {
        try {
            props = new Properties();
            props.load(getClass().getClassLoader().getResourceAsStream(PROPERTY_FILE));
        } catch (IOException e) {
            throw new RuntimeException("系统资源文件访问错误:"+e.getMessage());
        }
    }

    /**
     * 确定路径的用这个方法麻烦,底层是
     * URL url = new URL(location);
       return new UrlResource(url);
        跟如上方法没有差别
     */
    private void ConfigManagertest() {
        props = new Properties();
        String location = "classpath:/sharepoint/sharepoint.properties";
        InputStream is = null;
        ResourcePatternResolver patternResolver = new PathMatchingResourcePatternResolver();
        Resource[] resourceArr;
        try {
            resourceArr = patternResolver.getResources(location);
            for (Resource resource : resourceArr) {
                try {
                    is = resource.getInputStream();
                    props.load(new InputStreamReader(is, "UTF-8"));
                } catch (IOException ex) {
                    System.out.println("Could not load properties from path:" + location + ", " + ex.getMessage());
                } finally {
                    IOUtils.closeQuietly(is);
                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /**
     * 单例模式,获取ConfigManager的引用
     * @return ConfigManager
     */
    public static ConfigManager getInstance() {
        if (instance == null) {
            instance = new ConfigManager();
        }
        return instance;
    }
    /**
     * 根据参数名获取系统参数值
     * @param parameterName String
     * @return String
     */
    public static String getParameter(String parameterName) {
        return props.getProperty(parameterName);
    }
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值