본문 바로가기
개발/개발에 유용한 팁

MessageBundle DB로 전환

by 카리3 2020. 8. 23.

1. 테이블 생성

--테이블 생성
CREATE TABLE ST_COM_MESSAGE(
  ID bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT 'ID',
  CREATE_DATE TIMESTAMP DEFAULT NOW() COMMENT '생성일',
  CREATE_USER VARCHAR(50) COMMENT '생성자',
  UPDATE_DATE TIMESTAMP COMMENT '수정일',
  UPDATE_USER VARCHAR(50) COMMENT '수정자',
  DELETE_YN VARCHAR(1) DEFAULT 'N' COMMENT '삭제여부',
  MESSAGE_CODE VARCHAR(100) COMMENT '메시지코드',
  LABLE_KO VARCHAR(100) COMMENT '메시지_한글',
  LABLE_EN VARCHAR(100) COMMENT '메시지_영문'
) COMMENT='리소브번들 메시지 테이블' ENGINE=InnoDB AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

--INSERT문
INSERT INTO seckimdb.st_com_message
(CREATE_DATE, CREATE_USER, UPDATE_DATE, UPDATE_USER, DELETE_YN, MESSAGE_CODE, LABLE_KO, LABLE_EN)
VALUES ( now(), 'ADMIN', now(), 'ADMIN', 'N', 'LABEL.APPNAME', '강공', 'KANGONG');

2. 서비스 생성

--쿼리 생성
<select id="selectMessage" resultType="secretary.common.model.MessageVo">
        SELECT ID, CREATE_DATE createDate, CREATE_USER createUser, UPDATE_DATE updateDate, UPDATE_USER updateUser,
        	DELETE_YN deleteYn, MESSAGE_CODE messageCode, LABLE_KO lableKo, LABLE_EN lableEn
		FROM seckimdb.st_com_message M
		WHERE M.MESSAGE_CODE = #{messageCode}
 </select>


--서비스 생성
public MessageVo getMessageBundle(String messageCode, Locale locale) {
		MessageVo resultVo = sqlSession.selectOne("seckim.com.selectMessage", messageCode);
		if ("en".equals(locale.getLanguage())) {
			resultVo.setValue(resultVo.getLableEn());
		} else {
			resultVo.setValue(resultVo.getLableKo());
		}

		return resultVo;
	}

3. DatabaseMessageSource 생성

package secretary.common.util;

import java.text.MessageFormat;
import java.util.Locale;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;

import secretary.common.model.MessageVo;
import secretary.common.service.CommonService;

public class DatabaseMessageSource extends ReloadableResourceBundleMessageSource {

	@Autowired
	private CommonService commonService;

	@Override
	protected MessageFormat resolveCode(String code, Locale locale) {

		MessageVo messageVo = commonService.getMessageBundle(code, locale);

		MessageFormat format;

		if (messageVo != null && messageVo.getId() != null) {

			format = new MessageFormat(messageVo.getValue(), locale);

		} else {

			format = super.resolveCode(code, locale);

		}

		return format;

	}

	@Override
	protected String resolveCodeWithoutArguments(String code, Locale locale) {

		MessageVo messageVo = commonService.getMessageBundle(code, locale);

		String format;

		if (messageVo != null && messageVo.getId() != null) {

			format = messageVo.getValue();

		} else {

			format = super.resolveCodeWithoutArguments(code, locale);

		}

		return format;

	}

}

4. servlet-context.xml 설정

	
	<beans:bean id="messageSource" class="secretary.common.util.DatabaseMessageSource">
	    <beans:property name="basenames" value="classpath:defaultMessages"/>
	    <beans:property name="defaultEncoding" value="UTF-8"/>
	</beans:bean>
    
<!-- 리소스 디렉토리 아래의 message.properties를 MessageSource에 포함시킨다. -->
<!--
	<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
		<beans:property name="basename" value="classpath:message/message"/>
		<beans:property name="defaultEncoding" value="UTF-8" />
		<beans:property name="cacheSeconds" value="60"/>
	</beans:bean>
-->

'개발 > 개발에 유용한 팁' 카테고리의 다른 글

Regular Expression  (0) 2021.03.07
Mybatis 쿼리 바인딩 로그 출력  (0) 2020.08.24
Mysql vo 만들기  (0) 2020.08.15
톰캣 재시작 없이 Java 수정  (0) 2020.08.15
테이블로 VO, Insert, Update문 만들기  (0) 2020.08.13