Spring UTF-8 message resource from external jar issue

不羁的心 提交于 2019-12-23 05:47:06

问题


I have a problem with UTF-8 message sources in Spring MVC application. I've tried two implementations of AbstractMessageSource: ResourceBundleMessageSource and ReloadableResourceBundleMessageSource. I have an external jar with i18n messages contained in com.mypackage.i18n package

The configuration for ResourceBundleMessageSource:

<bean id="propertiesMessageSource"
    class="org.springframework.context.support.ResourceBundleMessageSource">
    <property name="basename" value="com.mypackage.i18n.messages" />
    <property name="useCodeAsDefaultMessage" value="true" />
</bean>

This configuration loads finds and loads properties, but fails with UTF-8, because this implementation just doesn't support UTF-8.

The configuration for ReloadableResourceBundleMessageSource:

<bean id="propertiesMessageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="com.mypackage.i18n.messages" />
    <property name="useCodeAsDefaultMessage" value="true" />
    <property name="fileEncodings" value="UTF-8" />
    <property name="defaultEncoding" value="UTF-8" />
</bean>

This configuration doesn't find properties. I know that this implementation to have reloadable resources needs properties to be located somewhere in WEB-INF directory and it doesn't restrict resources to be located somewhere else if you don't need reloading of resources. According to the class java:

Note that the base names set as "basenames" property are treated in a slightly different fashion than the "basenames" property of ResourceBundleMessageSource. It follows the basic ResourceBundle rule of not specifying file extension or language codes, but can refer to any Spring resource location (instead of being restricted to classpath resources). With a "classpath:" prefix, resources can still be loaded from the classpath, but "cacheSeconds" values other than "-1" (caching forever) will not work in this case.

Can someone advice me how to solve the problem: I need to use another approach or somehow modify config of ReloadableResourceBundleMessageSource to find resources from jar?


回答1:


I've found a solution. Proper ReloadableResourceBundleMessageSource configuration looks like this:

<bean id="propertiesMessageSource"
    class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="classpath:com/mypackage/i18n/messages" />
    <property name="useCodeAsDefaultMessage" value="false" />
    <property name="fileEncodings" value="UTF-8" />
    <property name="defaultEncoding" value="UTF-8" />
    <property name="cacheSeconds" value="-1"/>
</bean>


来源:https://stackoverflow.com/questions/24956584/spring-utf-8-message-resource-from-external-jar-issue

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!