spring로 검색한 결과 :: 시소커뮤니티[SSISO Community]
 
SSISO 카페 SSISO Source SSISO 구직 SSISO 쇼핑몰 SSISO 맛집
추천검색어 : JUnit   Log4j   ajax   spring   struts   struts-config.xml   Synchronized   책정보   Ajax 마스터하기   우측부분

회원가입 I 비밀번호 찾기


SSISO Community검색
SSISO Community메뉴
[카페목록보기]
[블로그등록하기]  
[블로그리스트]  
SSISO Community카페
블로그 카테고리
정치 경제
문화 칼럼
비디오게임 스포츠
핫이슈 TV
포토 온라인게임
PC게임 에뮬게임
라이프 사람들
유머 만화애니
방송 1
1 1
1 1
1 1
1 1
1

spring로 검색한 결과
등록일:2008-06-11 16:40:26
작성자:
제목:MessageSource 사용하기(다국어 지원)


ApplicationContext 인터페이스는 MessageSource 인터페이스를 상속하여 메세징 기능을 제공 합니다.
MessageSource에 정의된 메소드 입니다

String getMessage(String code, Object[] args, String default, Locale loc)
-가장 기본적인 메소드 입니다. 특정 로케일내에서 발견되는 메세지가 없다면
디폴트 메세지가 반환 됩니다.

String getMessage(String code, Object[] args, Locale loc)
-위 메소드와 동일 합니다만 디폴트 메세지가 없기 때문에 메세지가 발견 되지 않는다면 NoSuchMessageException 던져 집니다.

String getMessage(MessageSourceResolvable resolvable, Locale locale)
-위 메소드의 모든 프로퍼티를
MessageSourceResolvable에 설정해 사용 합니다.

ApplicationContext 컨테이너가 초기화 될 때 MessageSource를 구현한 bean을 찾아서 내부 프로퍼티 인
MessageSource messageSource에 등록 합니다.

이 과정에서 두가지 문제점이 있습니다.
첫번째로 MessageSource 타입의 bean을 찾는것이 아니라 bean의 id가 "messageSource" 인 것을
찾습니다. 설령 MessageSource의 구현체를 bean으로 등록했다 하더라도 id가 "messageSource" 가 아니라면 MessageSource가 없다고 판단 합니다.

두번째로 bean의 id가 "meesageSource"일 경우 즉시 MessageSource로 캐스팅을 시도 하기 때문에 MessageSource의 구현체가 아니라면 BeanNotOfRequiredTypeException이 던져지고 컨테이너 초기화에 실패 하게 됩니다.

위의 두가지 문제가 없이 발견 된다면 내부 프로퍼티인 messageSource 에게 모든 메세징 작업이 위임 될 것입니다. 만약 MessageSource bean이 발견되지 않는다면 어떻게 될까요?
ApplicationContext의 메세징 작업들은 내부 프로퍼티인 messageSource 위임 되어 있는
상태이기 때문에 반드시 어떠한 구현체가 하나라도 등록이 되어야 합니다.
(null인 상태라면 IllegalStateException이 납니다.)
그러한 이유로 메세지가 없이 비어 있는 DelegatingMessageSource 가 생성 되어 등록 됩니다.
(DelegatingMessageSource는 컴포지션으로 내부에 MessageSource를 가지고 위임하고 있는데 저걸 어떻게 이용 할 수 있는지는 잘 모르겠습니다. 참고로 여러가지 상황으로 테스트 해봤는데 내부의 MessageSource는 항상 null 입니다. 지극히 제 생각인데 컨테이너 초기화 로직에 의한 MessageSource할당이 불가능 하므로 별도의 코드로 사용자가 MessageSource를 주입해줘야 될 것 같습니다.)

spring에는 두개의 MessageSource구현체가 존재 합니다.ResourceBundleMessageSource와 StaticMessageSource인데 래퍼런스에 따르면 후자는 거의 사용되지 않는 다는군요.

다국어를 지원하는 간단한 예제를 보겠습니다.
한국과 미국의 간단한 message 파일 입니다.
파일명에 추가로 _ko_KR 과 _en_US는 다국어를 지원하기 위해 필요한 네이밍 규칙 입니다.
message_ko_KR.properties
message = \uc548\ub155 \ub0b4 \uc774\ub984\uc740 {0} \ub77c\uace0 \ud574
message_en_US.properties
message = hi my name is {0}

XML설정 파일 입니다.
  1. <bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">  
  2.         <property name="basenames">  
  3.             <list>  
  4.                 <value>message</value>  
  5.                 <value>message</value>  
  6.             </list>  
  7.         </property>  
  8. </bean>  
  9. <TextArea>  

테스트 파일 입니다.
  1. public class MessageTest {   
  2.     public static void main(String[] args) {   
  3.         ApplicationContext context = new ClassPathXmlApplicationContext(new String[] {"jjaeko/Beans.xml"});   
  4.         MessageSource messageSource = (MessageSource) context;   
  5.           
  6.         String korMessage = messageSource.getMessage("message"new String[] {"째코"}, "no surch", Locale.KOREA);   
  7.         String usMessage = messageSource.getMessage("message"new String[] {"jjaeko"}, "no surch", Locale.US);   
  8.           
  9.         System.out.println(korMessage);   
  10.         System.out.println(usMessage);   
  11.     }   
  12. }  

실행결과.
안녕 내 이름은 째코 라고 해
hi my name is jjaeko

일반적인 bean에서 MessaageSourceAware 인터페이스를 구현 하면 bean 생성 시점에서 MessageSource가 주입 될 것입니다.