본문 바로가기
Programming/Java

java config & message & properties & MessageSourceAccessor

by Chan_찬 2016. 4. 22.
728x90
반응형

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"));


728x90
728x90
BIG
Buy me a coffeeBuy me a coffee

댓글