singleton+static块

单例模式的意思就是只有一个实例。单例模式确保某一个类只有一个实例,而且自行实例化并向整个系统提供这个实例。这个类称为单例类。

package com;

import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

public class GetProperties {
    private static Properties properties;
    
    static {
        properties = new Properties();
        InputStream inStream = GetProperties.class.getResourceAsStream("/bocpoint.properties");
        try {
            properties.load(inStream);
        } catch(IOException ex) {
            ex.printStackTrace();
        }
    }
    
    public static String getValue(String key) {
        String str = null;
        if(null != properties) {
            str = properties.getProperty(key);
        }
        return str;
    }
}

posted on 2011-12-28 15:31  lovebeauty  阅读(253)  评论(0编辑  收藏  举报

导航