Site Search :
Standard Enterprise XML Methodology Pattern Setting Tunning Other
Article Contributors
GuestBook
Javapattern Maven
XSourceGen Dev
JetSpeed Test
JLook Image
jLook Family Site


Property Utility Examples
 
ÇÁ·ÎÁ§Æ®½Ã¿¡ xmlÀÌ ¾Æ´Ñ PropertiesŬ·¡½º¸¦ ÀÌ¿ëÇÑ È¯°æ¼³Á¤ ¹× µ¥ÀÌÅͷε忡 ´ëÇÑ ³»¿ªÀ» factory¹× object¿¡ ´ëÇÑ ¸Þ¸ð¸® ÀúÀåÀ» ÀÌ¿ëÇÏ¿© È¿°úÀûÀ¸·Î Àо ¼ö ÀÖ´Â ¹æ¾ÈÀ» ¸¶·ÃÇغ¸µµ·Ï ÇÑ´Ù. ( 2003/03/01 ) 451
Written by ienvyou - ÃÖÁö¿õ
2 of 2
 
ÀÚ ÀÌÁ¦ ±×·¯¸é À§ÀÇ static intializerºÎºÐ¿¡¼­ Á¢±ÙÀ» ½ÃµµÇÏ´Â 
factoryŬ·¡½º¸¦ º¸µµ·Ï ÇÏÀÚ. ÆÑÅ丮´Â ¸»±×´ë·Î °øÀåÀÌ´Ù. Áï ¾î¶°ÇÑ ºÎºÐ¿¡ À־
°´Ã¼¿¡ ´ëÇÑ »ý¼ºÀ» ´ã´çÇϴ Ŭ·¡½º¶ó°í º¼¼öµµ ÀÖ´Ù. ±âº»ÀûÀÎ µðÀÚÀÎÆÐÅÏ¿¡¼­´Â
³»°¡ ¿øÇϴ Ŭ·¡½º¿¡ ´ëÇÑ newInstance¸¦ È£ÃâÇϵµ·Ï ÇÏ°í ÀÖ´Â º¸Åë ¿ì¸®´Â 
StringÀÇ ¹®ÀÚ¿­À» ÀÌ¿ëÇÏ¿© ÇØ´ç Ŭ·¡½ºÀÇ À̸§À» °¡Áö°í reflectionÀ» ÀÌ¿ëÇÏ¿© »ý¼ºÇÏ´Â
°ÍÀÌ ÀϹÝÀûÀÎ ÇüÅÂÀÌ´Ù.

¾Æ·¡´Â PropertyFactoryŬ·¡½ºÀÇ ¼Ò½ºÆÄÀÏÀÌ´Ù.


package com.common.util;

import java.util.Properties;
import java.util.Hashtable;
import java.io.FileInputStream;


public class PropertyFactory {
	private static Hashtable propertyTable = new Hashtable();
	private static PropertyFactory factory;

	//¿ì¼± ¿©±â¼­ »ç¿ëÇϴ°ÍÀº 4°³ÀÇ ÇÁ·ÎÆÛƼ¸¦ »ç¿ëÇÏ´Â °ÍÀ¸·Î 
	//°¡Á¤À» ÇÑ´Ù. (query, environment, webpages, message);
	private Properties query;
	private Properties environment;
	private Properties webPages;
	private Properties message;

	public Hashtable getLoadProperties() {
		if(factory==null){
			synchronized(PropertyFactory.class){
				factory = new PropertyFactory();
				propertyTable = factory.init();
			}
		}
		return propertyTable;
	}

	//ÇØ´ç ÇÁ·ÎÆÛƼÆÄÀϵéÀ» ·ÎµùÇÑ´Ù.
public synchronized Hashtable init() {

//Properties Class¸¦ »ó¼Ó¹ÞÀº Ŭ·¡½ºÀÇ °´Ã¼¸¦ Á¤ÀÇÇÏ¿© 
//±× ŸÀÔÀ¸·Î ¸¸µçÈÄ ·ÎµùÀ» ½ÃµµÇϵµ·Ï ÇÑ´Ù.
CommonProperties prop = new PropertiesImpl();

try{
	for(int i = 0 ; i < Constant.propertyArray.length; i++){
		String name = Constant.propertyArray[i];
		FileInputStream fin = new FileInputStream(name + ".properties");
		prop.load(fin);
		
		//³ªÁß¿¡ Áö¿ì¼¼¿ä.
		prop.list(System.out);
		
		fin.close();
		propertyTable.put(name, prop);
	}
}catch(Exception e) {
	System.out.println("File Not Found in Factory.init()");
}


return propertyTable;
}
};
		
À§ÀÇ init()¸Þ¼Òµå¿¡¼­´Â ½ÇÁ¦ ÇÁ·ÎÆÛƼÆÄÀÏÀ» ÆÄÀÏ·Î ºÎÅÍ ÀÔ·ÂÀ» ¹Þµµ·Ï ÇÏ°í Àִµ¥
È®ÀåÀÚ .properties¿¡ ´ëÇÑ µð·ºÅ丮ÀÇ Á¤º¸¸¦ ¸ðµÎ Àоîµé¾î ±× °¹¼ö¸¸Å­À» 
property table¿¡ ÀԷ½ÃÅ°´Â °úÁ¤À» º¸¿©ÁÖ°í ÀÖ´Ù. 
CommonPropertiesŬ·¡½º´Â PropertiesŬ·¡½º¸¦ »ó¼Ó¹ÞÀº ¿ì¸®°¡ Á¤ÀÇÇÏ°íÀÚ ÇÏ´Â 
»õ·Î¿î ÇüÅÂÀÇ Å¬·¡½º¸¦ À§ÇÏ¿© ¿À¹ö¶óÀ̵ù½ÃÅ°±â À§ÇÑ Å¬·¡½ºÀ̸ç, 
customizingµÇ¾îÁ® Àִ Ŭ·¡½ºÀÌ´Ù.

CommonPropertiesŬ·¡½º´Â abstractŬ·¡½º·Î¼­ °¢°¢¿¡ ´ëÇÑ proxyÀÇ ¿ªÇÒÀ» ÇÒ ¼ö ÀÖ´Ù°í
º¸¾Æµµ ¹«¹æÇÒ °ÍÀ̸ç,  ½ÇÁ¦ÀÇ ±¸Çöü´Â PropertiesImplŬ·¡½ºÀÌ´Ù.
¾Æ·¡¿¡ ¼Ò½ºÄڵ带 ³ª¿­ÇÒ °ÍÀÌ´Ù.

* CommonProperties.java

package com.common.util;

import java.util.Properties;
import java.util.Hashtable;
import java.io.IOException; 
import java.util.Enumeration;
import java.io.InputStream;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;

public abstract class CommonProperties extends Properties{

public void load(InputStream inStream) {
	try{
		super.load(inStream);
	}catch(IOException e) {
		System.out.println("IOException in Properties when loading...");
	}
}

public String getProperty(String key) {
	return super.getProperty(key);
}

public String getProperty(String key, String defaultValue){
	return super.getProperty(key, defaultValue);
}

public void list(PrintStream out) {
	super.list(out);
}

public void list(PrintWriter out) {
	super.list(out);
}
public Enumeration  propertyNames() {
	return super.propertyNames();
}
public Object setProperty(String key, String value) {
	return super.setProperty(key, value);
}

public void store(OutputStream out, String header)
	throws IOException {
	super.store(out, header);
}
	   
	   
public abstract void init(String propertyFile) 
	throws Exception;

public abstract void init(String propertyFile, String encType)
	throws Exception;

public  abstract String[] getStringArray(String propertyName);

public abstract  boolean getBoolean(String propertyName);

public abstract  int getInt(String propertyName);

public abstract double getDouble(String propertyName);

public abstract  long getLong(String propertyName);

public abstract  String getString(String propertyName);

public abstract  void setString(String propertyName, 
		String propertyValue);


}


³ª¸ÓÁö PropertiesImpl.java´Â referenceÀÇ ¼Ò½ºÄڵ带 ÂüÁ¶Çϱ⠹ٶõ´Ù.

À§¿Í °°Àº ÇüÅÂÀÇ ÇÁ·ÎÆÛƼ¸¦ ÀÌ¿ëÇÏ¿© º¸Åë À¥¿¡ ´ëÇÑ ÀÛ¾÷À» ÇÏ°Ô µÈ´Ù¸é
°£´ÜÇÏ°Ô È¯°æ¼³Á¤µî¿¡ ´ëÇÑ ¼Ó¼º°ªµéÀ» Àо¼ö ÀÖÀ¸¸ç, ½±°Ô ÀÌ¿ëÇÒ ¼ö ÀÖ´Ù´Â
Ư¡À» °¡Áö°Ô µÉ°ÍÀÌ´Ù.


	
Written by carouser 2001-07-12
 
1 2
References
 
This article Source Code : properties.zip
Copyright ¨Ï 2003 www.javapattern.info & www.jlook.com, an jLOOK co.,LTD