src/main/resources/messages_ko.properties
config.1 = 설정1
config.2 = 설정2
src/main/java/.../PropertyMessage.java
package project.common;
import org.springframework.context.support.MessageSourceAccessor;
public class PropertyMessage {
static MessageSourceAccessor messageSourceAccessor;
public MessageSourceAccessor getMessageSourceAccessor() {
return messageSourceAccessor;
}
public void setMessageSourceAccessor(MessageSourceAccessor messageSourceAccessor) {
this.messageSourceAccessor = messageSourceAccessor;
}
public static String getMessage(String key) {
return messageSourceAccessor.getMessage(key);
}
}
webCinfiguration //bean 등록
@Configuration
public class commonConfiguration {
@Bean
public ReloadableResourceBundleMessageSource messageSource(){
ReloadableResourceBundleMessageSource messageSource = new
ReloadableResourceBundleMessageSource();
messageSource.setBasename("classpath:messages");
return messageSource;
}
@Bean
public MessageSourceAccessor messageSourceAccessor() {
return new MessageSourceAccessor(messageSource());
}
}
사용
logger.info("property:{}", PropertyMessage.getMessage("config.1"));
댓글