국제화 (i18n) 기능을 제공하는 인터페이스
ApplicationContext extends MessageSource
@Component
public class AppRunner implements ApplicationRunner {
/*
@Autowired
MessageSource messageSource;
*/
@Autowired
ApplicationContext messageSource;
@Override
public void run(ApplicationArguments args) throws Exception {
System.out.println(messageSource.getClass());
System.out.println(messageSource.getMessage("greeting", new String[]{"spring"}, Locale.KOREA));
System.out.println(messageSource.getMessage("greeting", new String[]{"spring"}, Locale.getDefault()));
}
}
리로딩 기능이 있는 메시지 소스 사용하기
@Bean
public MessageSource messageSource() {
var messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:/messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(3);
return messageSource;
@SpringBootApplication
public class Springtest5Application {
public static void main(String[] args) {
SpringApplication.run(Springtest5Application.class, args);
}
@Bean
public MessageSource messageSource(){
var messageSource = new ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:/messages");
messageSource.setDefaultEncoding("UTF-8");
messageSource.setCacheSeconds(3);
return messageSource;
}
}
@Component
public class AppRunner implements ApplicationRunner {
/*
@Autowired
MessageSource messageSource;
*/
@Autowired
ApplicationContext messageSource;
@Override
public void run(ApplicationArguments args) throws Exception {
while(true){
System.out.println(messageSource.getMessage("greeting", new String[]{"spring"}, Locale.KOREA));
System.out.println(messageSource.getMessage("greeting", new String[]{"spring"}, Locale.getDefault()));
Thread.sleep(1000);
}
}
}
이렇게 사용하면 message 소스를 바꾼 후 build하면 message를 바꿀 수도 있다.
참고