Summary
- SpringBoot ๋ฅผ ์ฌ์ฉํ๋ค๋ฉด application.properties ๋ฅผ ์ ์ธํด์ MessageSource ๋ฅผ ๊ด๋ฆฌํ์.
- MessageAutoConfiguration ์ MessageSource ๋น์ ์ ์ธํ์ง ์์ผ๋ฉด ์๋ ์ค์ ๋๋ค.
- application.properties ์์ spring.messages ๋ก ์์ํ๋ property ๋ฅผ ํ์ํด์ MessageSource ๋ฅผ ์์ฑํ๋ค.
1. MessageSource ์ค์ ๋ฐ ์ ์ธ ๋ฐฉ๋ฒ
[1] MessageSource ์ ์ธ ๋ฐฉ๋ฒ
(1) application.properties ์ ์ธํ๋ ๋ฐฉ๋ฒ
spring.messages.basename=message/messages
spring.messages.encoding=UTF-8 # default
spring.messages.fallbackToSystemLocale=false
(2) MessageSource Bean ์ ์ธ
@Configuration
public class MessageSourceConfig {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("customMessage/messages");
messageSource.setFallbackToSystemLocale(false);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
[2] message ์ ์ธ ๋ฐฉ๋ฒ
- basename ํ์์ message_์ธ์ด์ฝ๋_๊ตญ๊ฐ์ฝ๋.properties(.xml) ์ ์ธํ๊ธฐ

2. MessageSourceAutoConfiguration ์ ํ์ธํด๋ณด์
[1] META-INF/spring/spring-autoconfigure-metadata.properties


- META-INF/spring/spring-autoconfigure-metadata.properties ๋ฅผ ํ์ธํด๋ณด๋ฉด MessageSourceAutoConfiguration ์ด ์กด์ฌํ๋ ๊ฒ์ ํ์ธ
[2] MessageSourceAutoConfiguration ํ์ฑ ์กฐ๊ฑด ํ์ธ
- ์์น : org.springframework.boot.autoconfigure.context.MessageSourceAutoConfiguration

@ConditionalOnMissingBean(name = AbstractApplicationContext.*MESSAGE_SOURCE_BEAN_NAME*, search = SearchStrategy.*CURRENT*)
- messageSource bean ์ด๋ฆ์ด ์กด์ฌํ์ง ์์ ๊ฒฝ์ฐ
- SearchStrategy
- BeanFactory ๊ณ์ธต ๊ตฌ์กฐ์์ bean ์ ๋ํ ๋ช ๋ช ๋ ๊ฒ์ ์ ๋ต.
- CURRENT : Search only the current context.
@Conditional(ResourceBundleCondition.class)
- ResourceBundleCondition ์ ์กฐ๊ฑด์ ๋ง์กฑํ ๊ฒฝ์ฐ, MessageSourceAutoConfiguration ์ ์คํํ๋ค.
(1) ResourceBundleCondition
getMatchOutcome

- basename ์ application.properties(application.yml) ์์ spring.messages.basename ์์ฑ์ ๋ํ ๊ฐ์ ๊ฐ์ ธ์จ๋ค. ์์ ๊ฒฝ์ฐ, “messages” ๋ฌธ์์ด ๋ฐํ
- cache ์ basename ์ผ๋ก ์ ์ฅ๋ ๊ฐ์ด ์๋์ง ํ์ธํ๋ค. ๋ง์ฝ ์ ์ฅ๋ ๊ฐ์ด ์์ ๊ฒฝ์ฐ, getMatchoutcomeForBasename ์ ํธ์ถํ๊ณ ๊ฒฐ๊ณผ ๊ฐ์ outcome ์ ํ ๋นํ๋ค.
- outcome ์ cache ๋ณ์์ ํ ๋นํ๋ค.
getMatchoutcomeForBasename

- basename ๋ฆฌ์คํธ๋ฅผ ๋ง๋ค๊ณ getResources ์ ํด๋น basename ์ ๋ด์ ํธ์ถํ๋ค.
- getResource() ๋ฆฌํด ๊ฐ์ธ Resource ์ ๊ฐ ์กด์ฌ์ ๋ฌด๋ฅผ ํ์ธํ๋ค.
- ์กด์ฌํ๋ ์์์ด ์๋ค๋ฉด, match = true ์ธ outcome์ ๋ฐํํ๋ค.
getResources

- classpath ๋ด์ basename์ ํด๋นํ๋ properties ํ์ผ์ด ์๋์ง ์ฐพ์์ ๋ฐํํ๋ค.
- classpath ๋ด์ basename์ ํด๋นํ๋ properties ํ์ผ์ด ์๋ค๋ฉด ์๋ ์ค์ ์ด ์๋๋ค.
- (๋ฐ๋ผ์ messages.properties ์ ๊ฐ์ ๊ธฐ๋ณธ ํ์ผ์ ์ค์ ํด๋๋ ๊ฒ์ด ์ข์ ๋ฏ ํ๋ค.)
[3] MessageSource ์์ฑ ์กฐ๊ฑด์ ํ์ธํด๋ณด์.

- MessageSourceProperties ๋ฅผ ์ค์ ํ๊ณ MessageSource ์ ๋น์ผ๋ก ์์ฑํ๋ค.
[4] MessageSource : AutoConfiguration vs Custom
(1) AutoConfiguration : properties ๊ธฐ๋ฐ ์ค์
spring.messages.basename=message/messages
spring.messages.encoding=UTF-8
spring.messages.fallbackToSystemLocale=false
- fallbackToSystemLocale : ์์ฒญ๋ฐ์ locale ์ ๋ํ ๋ฉ์ธ์ง ํ์ผ์ด ์์ ๋, ์์คํ
locale์ ์ฌ์ฉํ ๊ฒ์ธ์ง์ ๋ํ ์ต์
- true : ํ๊ฒฝ์ค์ ์ธ์ด์ ํด๋นํ๋ ์์ด์ฉ ๋ฉ์ธ์ง ํ์ผ์ ๊ฐ์ ธ์จ๋ค.
- false : ๊ธฐ๋ณธ ๋ฉ์ธ์ง ํ์ผ์์ ๋ฉ์ธ์ง๋ฅผ ๊ฐ์ ธ์จ๋ค.
(2) Custom : MessageSource ๋น ์ ์ธ
@Configuration
public class MessageSourceConfig {
@Bean
public MessageSource messageSource() {
ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setBasenames("customMessage/messages");
messageSource.setFallbackToSystemLocale(false);
messageSource.setDefaultEncoding("UTF-8");
return messageSource;
}
}
(3) ๊ฒฐ๋ก : Custom ์ค์ ์ ๊ฐ์ ธ์จ๋ค.
@RequiredArgsConstructor
@RestController
public class testController {
private final MessageSource messageSource;
@GetMapping()
public ResponseEntity<String> printHello() {
return ResponseEntity.status(200).body(messageSource.getMessage("hello", null, LocaleContextHolder.getLocale()));
}
}
- MessageSourceAutoConfiguration ์์ messageSource ์ด๋ฆ์ผ๋ก ์์ํ๋ MessageSource ์ด ์์ผ๋ฉด ์๋ ์ค์ ์ด ํ์ฑํ๋์ง ์๋๋ค.
// ์ฌ๊ธฐ
@ConditionalOnMissingBean(name = AbstractApplicationContext.MESSAGE_SOURCE_BEAN_NAME, search = SearchStrategy.CURRENT)
public class MessageSourceAutoConfiguration {
Reference
'๐ backend > spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
| Spring REST Docs + Swagger UI ๋ฅผ ํตํ API ๋ฌธ์ํ (0) | 2024.12.07 |
|---|---|
| ๋น๊ด์ ๋ฝ vs ๋๊ด์ ๋ฝ(In JPA) (0) | 2024.11.28 |
| [kurly tech blog] Redisson, Spring AOP ๊ธฐ๋ฐ ๋ถ์ฐ๋ฝ ์ ์ฉ ๋ฐฉ๋ฒ summary (0) | 2024.11.26 |
| Spring Batch ๊ธฐ๋ฐ ํ์ ์ญ์ ๋ฐฐ์น ์ฝ์ง ๊ธฐ๋ก (0) | 2024.09.03 |
| [Spring Batch] Job ์คํ ํ๋กํผํฐ, JobParameter, Scope (0) | 2024.08.26 |
