1. 程式人生 > >根據properties配置檔案獲取裡面的鍵值對,jfinal原始碼分析

根據properties配置檔案獲取裡面的鍵值對,jfinal原始碼分析

在jfinal框架中,有一個Prop的類,該類提供對properties配置檔案裡獲取鍵值對的功能。

其底層還是用的是java.util.Properties的相關方法,只不過是人家進行了封裝而已。牛人都喜歡封裝原有的東西。

其建構函式Prop(String fileName,String encoding)
以及Prop(File file,String encoding)

程式碼都很好理解,如下:

public Prop(String fileName, String encoding) {
		InputStream inputStream = null;
		try {
			inputStream = Thread.currentThread().getContextClassLoader().getResourceAsStream(fileName);		// properties.load(Prop.class.getResourceAsStream(fileName));
			if (inputStream == null)
				throw new IllegalArgumentException("Properties file not found in classpath: " + fileName);
			properties = new Properties();
			properties.load(new InputStreamReader(inputStream, encoding));
		} catch (IOException e) {
			throw new RuntimeException("Error loading properties file.", e);
		}
		finally {
			if (inputStream != null) try {inputStream.close();} catch (IOException e) {e.printStackTrace();}
		}
	}

其輸入引數傳遞File也可以

public Prop(File file, String encoding) {
		if (file == null)
			throw new IllegalArgumentException("File can not be null.");
		if (file.isFile() == false)
			throw new IllegalArgumentException("File not found : " + file.getName());
		
		InputStream inputStream = null;
		try {
			inputStream = new FileInputStream(file);
			properties = new Properties();
			properties.load(new InputStreamReader(inputStream, encoding));
		} catch (IOException e) {
			throw new RuntimeException("Error loading properties file.", e);
		}
		finally {
			if (inputStream != null) try {inputStream.close();} catch (IOException e) {e.printStackTrace();}
		}
	}

兩者的區別就是根據檔名獲取InputStream,一種是根據File物件獲取InputStream.

其中該類中有一個私有成員變數

private Properties properties = null;

從建構函式中可以看出,呼叫建構函式之後對該成員變數進行了賦值操作。
所以外層的程式碼就可以呼叫期間定義的封裝方法了。

如一系列的get方法,加預設值的等等,以及getProperties()返回該成員變數,以及是否包含該key的方法containsKey(String key),其直接呼叫Properties.containsKey(String key)

有了這個方法之後配合PropKit工具類,就可以發揮其強大的威力了。

使用方式:

PropKit.use("my_config.txt").get("userName")

其中get方法的實現如下:

	public static Prop getProp() {
		if (prop == null)
			throw new IllegalStateException("Load propties file by invoking PropKit.use(String fileName) method first.");
		return prop;
	}
	
	public static String get(String key) {
		return getProp().get(key);
	}

告訴我們需要先呼叫use方法,其他一系列的方法類似。
PropKit採用單例的模型,其建構函式私有,並內部靜態成員變數Prop prop,以及Map<String,Prop> ,採用惡漢式,先取再說,如果沒有再初始化。

private static Prop prop = null;
	private static final Map<String, Prop> map = new ConcurrentHashMap<String, Prop>();
	
	private PropKit() {}
public static Prop use(String fileName, String encoding) {
		Prop result = map.get(fileName);
		if (result == null) {
			result = new Prop(fileName, encoding);
			map.put(fileName, result);
			if (PropKit.prop == null)
				PropKit.prop = result;
		}
		return result;
	}

為了維護該Map,提供瞭如下兩個方法。

public static Prop useless(String fileName) {
		Prop previous = map.remove(fileName);
		if (PropKit.prop == previous)
			PropKit.prop = null;
		return previous;
	}
	
	public static void clear() {
		prop = null;
		map.clear();
	}