.properties 의 경우 ISO-8859-1 문자셋이기때문에 한글 사용에 문제가 있어, IDE Tool 을 사용하여 자동으로 변환하는 방법을 이용하곤 했다.

하지만 이 방법의 경우에도 서버상에 올라가있는 설정 파일을 확인 및 직접 수정할 경우 에로 사항이 발생한다.

또한 개개인별 .properties 를 ms949, euc-kr 등으로 변경 해 놓는 사태도 발생해버린다. OTL...


이러한 불편함을 해결 하기 위해 .xml 확장자를 이용하면 좋을것이다.



[applicationContext.xml]


	
	
        
            
                classpath:/properties/globals-properties.xml
            
        
    

	
		
			
				classpath:properties/messages-properties
			
		
	

	
	
		
			
		
	


[Sample.java]

@Controller
public class Sample {
	
	@Value("#{global['server.type']}") private String serverType;
	@Resource GlobalsProperties globalsProperties;

	@RequestMapping(value="/sample")
	public void test(ModelMap model) {
		// Properties Case 1
		System.out.println("serverType: " + serverType);

		// Properties Case 2
		System.out.println("server.type: " + globalsProperties.getProperty("server.type"));
			
		//MessageSource Case 1
		System.out.println("msg.test1: " + messageSource.getMessage("msg.test1", null, Locale.getDefault()));

		// MessageSource Case 2
		System.out.println("msg.test2: " + messageSource.getMessage("msg.test2", new String[]{"첫번째", "두번째"}, Locale.getDefault()));
	}
}


[sample.jsp]

	
	
	
	
		로컬임
	

	
	

	
	


globals-properties.xml




	globals-properties local
	local								

	result					
	message				


[messages-properties_ko.xml]




	messages-properties_ko


	ko 한글 정보는...
	ko 파라미터1 = {0}, 파라미터2 = {1}...


Properties 를 한번 사용할려고 여기저기, 자료를 찾을려고 기웃거려 봤는데..

워낙, 초보적인 내용이었는지.. 정리된것이 없더군요.


제가 가지고 있는 소스에 관련된 부분이 있어서.. 여기에 올립니다.

저초럼, 초보인 분에게 작은 도움이 될것 같아 올려봅니다.


......

간단히, 소스를 설명하자면..

PropertiesTest 실행할때

test.properties 라는 프로퍼티 파일을 가져오는데..

argument 가 있으면, test.properties 의 name 에 해당하는 value 를 불러오는거구요..

argument 가 없으면, test.properties 이의 프로퍼트에 value 을 변경시켜주는겁니다.~


test.properties 는 'PropertiesTest' class 가 있는 같은 디렉토리 위치에 있어야 겠지요.~


//===============================================================

//PropertiesTest.java
//===============================================================

import java.util.*;
import java.io.*;

class PropertiesTest {  

    public static void main(String[] args)  throws IOException   {
        Properties props = new Properties();
        if ( args.length == 0 )  {
            props.put("Name", "홍길동");
            props.put("Address", "Korea");
            props.store( new FileOutputStream("test.properties"), "My Comment");
        } else  {
            props.load( new FileInputStream("test.properties") );
        }

        System.out.println( "Name: " + props.getProperty("Name") );
        System.out.println( "Address: " + props.getProperty("Address") );
    }
}



//===============================================================

//test.properties

//===============================================================

#My Comment
Name=evergreen

Address=Korea

+ Recent posts