1.配置一个ReloadableResourceBundleMessageSource的bean:
<bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
<property name="basenames">
<list>
<!-- 在web环境中一定要定位到classpath 否则默认到当前web应用下找 -->
<value>classpath:i18n/messages/messages</value>
</list>
</property>
<property name="useCodeAsDefaultMessage" value="false"/>
<property name="defaultEncoding" value="UTF-8"/>
<property name="cacheSeconds" value="60"/>
</bean>
2.国家化消息文件:
image.png image.png
3.使用:
messageSource.getMessage(String.valueOf(result.getCode()), new Object[]{},Locale.SIMPLIFIED_CHINESE);
bean的设置只是定位到文件的前缀 messages
而后面的 zh_CN 由 Locale.SIMPLIFIED_CHINESE 获得。
网友评论