InternationalizatioN
Core Technologies
In the preceding scenario, using @Autowired works well and provides the desired modularity, but determining exactly where the autowired bean definitions are declared is still somewhat ambiguous. For example, as a developer looking at ServiceConfig, how do
docs.spring.io
만약 우리나라뿐만 아닌 해외로도 판매를 하는 쇼핑몰을 만든다고 하면 이미지는 다 똑같지만 언어는 달라야한다.
한국어버전의 파일, 영어버전의 파일 등을 따로 저장해서 보관한다.
1️⃣ 파일명_국가별언어코드.properties ⇒ 리소스번들(resource bundle) 파일 이라고 부른다.
< bean id=”messageSource” class=”ReloadableResourceBundleMessageSource” >
< property name=”basename” value=”classpath:com/config/파일명” >
</bean>
⭐주의할 점 2가지
- 반드시 id=”messageSource”
- 경로 지정시 .properties 쓰지 않고 ‘파일명’ 만 지정
⭐번들 파일 사용하는 대표적인 3가지 ⇒ 반드시 ApplicationContext 이용
- TestMain 에서는 ApplicationContext 이용
- 임의의 빈에서는 implements MessageSourceAware 하고 setMessageSource(MessageSource xxx) {} 재정의하면 자동으로 MessageSource가 주입된다.
(MessageSource와 ApplicationContext는 상속관계 )
( MessageSource를 상속받으면 ApplicationContext도 상속받는다는 것이다. -다형성 ) - JSP
한국어 버전
shop_ko.properties
⇒ greeting=안녕하세요 (greeting이 key)
영어 버전
shop_en.properties
⇒ greeting=hello
나중에 실제로 등록할때 파일명.properties 만 등록한다.
2️⃣ user.xml에 등록
( 주의할 점은 반드시 id값이 “messageSource” 이고 확장자는 지정안함 )
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basename" value="classpath:com/config/shop" />
</bean>
💡 id값이 “messageSource”
https://docs.spring.io/spring-framework/docs/5.2.25.RELEASE/spring-framework-reference/core.html#context-introduction
- properties 파일을 utf-8로 저장한 경우
<!-- properties 파일을 utf-8로 저장한 경우에 사용 -->
<property name="defaultEncoding" value="utf-8" />
3️⃣ 반드시 ApplicationContext를 가진 곳에서 사용이 가능하다.
⇒ TestMain.java에서 사용가능
MassageSource ctx = new GenericXmlApplicationContext(" ");
계층 구조 상 ApplicationContext 대신 MassageSource도 가능하다.
'Framework > SPRING FRAMEWORK' 카테고리의 다른 글
자바의 싱글톤패턴 구현 (0) | 2023.08.29 |
---|---|
TestMain이 아닌 일반클래스(빈, UserService)에서 사용하는 방법 (0) | 2023.08.29 |
스프링에서 jdbc.properties 내용 불러오기 (0) | 2023.08.29 |
profile (0) | 2023.08.29 |
현재 실행중인 운영체제의 환경변수 값 살펴보기 (0) | 2023.08.29 |