MessageSourceAutoConfiguration ์ฝ”๋“œ ๊ฒ€ํ† ํ•˜๊ธฐ

2024. 11. 28. 04:45ยท๐Ÿ“‚ backend/spring

Summary

  1. SpringBoot ๋ฅผ ์‚ฌ์šฉํ•œ๋‹ค๋ฉด application.properties ๋ฅผ ์„ ์–ธํ•ด์„œ MessageSource ๋ฅผ ๊ด€๋ฆฌํ•˜์ž.
    1. MessageAutoConfiguration ์€ MessageSource ๋นˆ์„ ์„ ์–ธํ•˜์ง€ ์•Š์œผ๋ฉด ์ž๋™ ์„ค์ •๋œ๋‹ค.
    2. 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*)
  1. messageSource bean ์ด๋ฆ„์ด ์กด์žฌํ•˜์ง€ ์•Š์„ ๊ฒฝ์šฐ
  2. SearchStrategy
    • BeanFactory ๊ณ„์ธต ๊ตฌ์กฐ์—์„œ bean ์— ๋Œ€ํ•œ ๋ช…๋ช…๋œ ๊ฒ€์ƒ‰ ์ „๋žต.
    • CURRENT : Search only the current context.

 

@Conditional(ResourceBundleCondition.class)
  • ResourceBundleCondition ์˜ ์กฐ๊ฑด์„ ๋งŒ์กฑํ•  ๊ฒฝ์šฐ, MessageSourceAutoConfiguration ์„ ์‹คํ–‰ํ•œ๋‹ค.

 

(1) ResourceBundleCondition

getMatchOutcome

  1. basename ์„ application.properties(application.yml) ์—์„œ spring.messages.basename ์†์„ฑ์— ๋Œ€ํ•œ ๊ฐ’์„ ๊ฐ€์ ธ์˜จ๋‹ค. ์—†์„ ๊ฒฝ์šฐ, “messages” ๋ฌธ์ž์—ด ๋ฐ˜ํ™˜
  2. cache ์— basename ์œผ๋กœ ์ €์žฅ๋œ ๊ฐ’์ด ์žˆ๋Š”์ง€ ํ™•์ธํ•œ๋‹ค. ๋งŒ์•ฝ ์ €์žฅ๋œ ๊ฐ’์ด ์—†์„ ๊ฒฝ์šฐ, getMatchoutcomeForBasename ์„ ํ˜ธ์ถœํ•˜๊ณ  ๊ฒฐ๊ณผ ๊ฐ’์„ outcome ์— ํ• ๋‹นํ•œ๋‹ค.
  3. outcome ์„ cache ๋ณ€์ˆ˜์— ํ• ๋‹นํ•œ๋‹ค.

 

getMatchoutcomeForBasename

  1. basename ๋ฆฌ์ŠคํŠธ๋ฅผ ๋งŒ๋“ค๊ณ  getResources ์— ํ•ด๋‹น basename ์„ ๋‹ด์•„ ํ˜ธ์ถœํ•œ๋‹ค.
  2. getResource() ๋ฆฌํ„ด ๊ฐ’์ธ Resource ์˜ ๊ฐ’ ์กด์žฌ์œ ๋ฌด๋ฅผ ํ™•์ธํ•œ๋‹ค.
  3. ์กด์žฌํ•˜๋Š” ์ž์›์ด ์žˆ๋‹ค๋ฉด, match = true ์ธ outcome์„ ๋ฐ˜ํ™˜ํ•œ๋‹ค.

 

getResources

  1. classpath ๋‚ด์— basename์— ํ•ด๋‹นํ•˜๋Š” properties ํŒŒ์ผ์ด ์žˆ๋Š”์ง€ ์ฐพ์•„์„œ ๋ฐ˜ํ™˜ํ•œ๋‹ค.
  2. classpath ๋‚ด์— basename์— ํ•ด๋‹นํ•˜๋Š” properties ํŒŒ์ผ์ด ์—†๋‹ค๋ฉด ์ž๋™ ์„ค์ •์ด ์•ˆ๋œ๋‹ค.
    1. (๋”ฐ๋ผ์„œ messages.properties ์™€ ๊ฐ™์€ ๊ธฐ๋ณธ ํŒŒ์ผ์„ ์„ค์ •ํ•ด๋‘๋Š” ๊ฒƒ์ด ์ข‹์„ ๋“ฏ ํ•˜๋‹ค.)

 

[3] MessageSource ์ƒ์„ฑ ์กฐ๊ฑด์„ ํ™•์ธํ•ด๋ณด์ž.

  1. MessageSourceProperties ๋ฅผ ์„ค์ •ํ•˜๊ณ  MessageSource ์„ ๋นˆ์œผ๋กœ ์ƒ์„ฑํ•œ๋‹ค.

 

[4] MessageSource : AutoConfiguration vs Custom

(1) AutoConfiguration : properties ๊ธฐ๋ฐ˜ ์„ค์ •

spring.messages.basename=message/messages
spring.messages.encoding=UTF-8
spring.messages.fallbackToSystemLocale=false
  1. fallbackToSystemLocale : ์š”์ฒญ๋ฐ›์€ locale ์— ๋Œ€ํ•œ ๋ฉ”์„ธ์ง€ ํŒŒ์ผ์ด ์—†์„ ๋•Œ, ์‹œ์Šคํ…œ locale์„ ์‚ฌ์šฉํ•  ๊ฒƒ์ธ์ง€์— ๋Œ€ํ•œ ์˜ต์…˜
    1. true : ํ™˜๊ฒฝ์„ค์ • ์–ธ์–ด์— ํ•ด๋‹นํ•˜๋Š” ์˜์–ด์šฉ ๋ฉ”์„ธ์ง€ ํŒŒ์ผ์„ ๊ฐ€์ ธ์˜จ๋‹ค.
    2. 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

  • https://velog.io/@youjung/Spring-MessageSource-์ž๋™์„ค์ •์œผ๋กœ-MessageSource-์‰ฝ๊ฒŒ-์„ธํŒ…ํ•˜๊ธฐ

'๐Ÿ“‚ 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
'๐Ÿ“‚ backend/spring' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€
  • Spring REST Docs + Swagger UI ๋ฅผ ํ†ตํ•œ API ๋ฌธ์„œํ™”
  • ๋น„๊ด€์  ๋ฝ vs ๋‚™๊ด€์  ๋ฝ(In JPA)
  • [kurly tech blog] Redisson, Spring AOP ๊ธฐ๋ฐ˜ ๋ถ„์‚ฐ๋ฝ ์ ์šฉ ๋ฐฉ๋ฒ• summary
  • Spring Batch ๊ธฐ๋ฐ˜ ํšŒ์› ์‚ญ์ œ ๋ฐฐ์น˜ ์‚ฝ์งˆ ๊ธฐ๋ก
cooper25
cooper25
  • cooper25
    dev cooper
    cooper25
  • ์ „์ฒด
    ์˜ค๋Š˜
    ์–ด์ œ
    • ๋ถ„๋ฅ˜ ์ „์ฒด๋ณด๊ธฐ (84)
      • ๐Ÿ“‚ backend (34)
        • spring (19)
        • architecture (10)
        • test (5)
      • ๐Ÿ“‚ computer-science (5)
      • ๐Ÿ“‚ programming-language (12)
        • java (12)
      • ๐Ÿ“‚ infra (9)
        • mysql (4)
        • redis (2)
        • message-queue (3)
      • ๐Ÿ“‚ cloud (2)
        • aws (2)
      • ๐Ÿ“‚ frontend (1)
        • react (0)
      • ๐Ÿ“‚ education & lecture (16)
        • ์ธํ”„๋ผ ๊ณต๋ฐฉ (11)
        • ํ•ญํ”Œ ๋ฐฑ์—”๋“œ 7๊ธฐ (5)
      • ๐Ÿ“‚ ai (1)
        • claude (1)
      • ๐Ÿ“‚ etc (2)
        • ํšŒ๊ณ  (1)
        • ์ปจํผ๋Ÿฐ์Šค (1)
  • ๋ธ”๋กœ๊ทธ ๋ฉ”๋‰ด

    • ํ™ˆ
    • ํƒœ๊ทธ
    • ๋ฐฉ๋ช…๋ก
  • ๋งํฌ

  • ๊ณต์ง€์‚ฌํ•ญ

  • ์ธ๊ธฐ ๊ธ€

  • ํƒœ๊ทธ

    ๋™์‹œ์„ฑ
    nGrinder
    react
    JPA
    ํ•ญํ•ดํ”Œ๋Ÿฌ์Šค
    ๋ฐ์ดํ„ฐ ์ค‘์‹ฌ ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์„ค๊ณ„
    spring camp 2025
    spring AOP
    ์ธํ”„๋ผ๊ณต๋ฐฉ
    AWS
    kafka
    UUID
    ์Šคํ”„๋ง์บ ํ”„ ํ›„๊ธฐ
    ํšŒ๊ณ 
    mysql
    ๊ฐ€์ƒ ๋ฉด์ ‘ ์‚ฌ๋ก€๋กœ ๋ฐฐ์šฐ๋Š” ๋Œ€๊ทœ๋ชจ ์„ค๊ณ„
    spring-batch
    spring
    Redisson
    gof
  • ์ตœ๊ทผ ๋Œ“๊ธ€

  • ์ตœ๊ทผ ๊ธ€

  • hELLOยท Designed By์ •์ƒ์šฐ.v4.10.6
cooper25
MessageSourceAutoConfiguration ์ฝ”๋“œ ๊ฒ€ํ† ํ•˜๊ธฐ
์ƒ๋‹จ์œผ๋กœ

ํ‹ฐ์Šคํ† ๋ฆฌํˆด๋ฐ”