Java 作为 spring MessageSource 的外部属性文件不起作用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/21036750/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 06:07:15  来源:igfitidea点击:

External properties file as spring MessageSource not working

javaspring

提问by Saurab Parakh

Consider below code:

考虑下面的代码:

<bean id="busmessageSource" 
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basenames">
        <list>
            <value>classpath:bundles/resource</value>
            <value>classpath:bundles/override</value>
            <value>file:/C:/mmt/override</value>
        </list>
    </property>
    <property name="cacheSeconds" value="100" />
</bean>

Here properties from bundles/resourceand bundles/overrideget fetched when I call busmessageSource.getMessage("anykey", null, null)

这里的属性来自bundles/resourcebundles/override在我调用时获取busmessageSource.getMessage("anykey", null, null)

but it fails when I try to fetch values for properties in C:/mmt/override

但是当我尝试为 中的属性获取值时它失败了 C:/mmt/override

  1. What is correct way of configuring messagesource with external file from the disk.
  2. Also I want file:/C:/mmt/overrideto override values in classpath:bundles/overrideif any with the same key exist. How do I override properties from an external file outside of my war folder?
  1. 使用磁盘中的外部文件配置消息源的正确方法是什么。
  2. 如果存在任何具有相同键的file:/C:/mmt/override值,我也想覆盖其中的值classpath:bundles/override。如何从我的 war 文件夹之外的外部文件覆盖属性?

回答by Armando

1.)I have these 3 ways:

1.)我有这 3 种方式:

<beans:bean id="messageSource" class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <beans:property name="basename">
        <beans:value>file:/path/to/messages</beans:value>
    </beans:property>
</beans:bean>

Note1: You mustuse the } file:prefix and the ReloadableResourceBundleMessageSourceclass.

注意 1:您必须使用 }file:前缀和ReloadableResourceBundleMessageSource类。

Note2: Do notput the ".properties" extension.

注2:做把“的.properties”扩展名。

2.)You override previous values when you load a new properties file with same property names (keys). You must ensure that you fetch last the properties file you want to use.

2.)当您加载具有相同属性名称(键)的新属性文件时,您会覆盖以前的值。您必须确保最后获取要使用的属性文件。

回答by Kamal Singh

You can try

你可以试试

<bean id="messageSource"
  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="basenames">
        <list>
            <value>classpath:bundles/resource</value>
            <value>classpath:bundles/override</value>
            <value>file:C:/mmt/override</value>
        </list>
    </property>
</bean>

About message resource keep note:

关于消息资源请注意:

  1. A plain path will be relative to the current application context.
  2. A "classpath:" URL will be treated as classpath resource.
  3. A "file:" URL will load from an absolute file system path.
  4. Any other URL, such as "http:", is possible too.
  1. 普通路径将相对于当前应用程序上下文。
  2. “classpath:” URL 将被视为类路径资源。
  3. “file:” URL 将从绝对文件系统路径加载。
  4. 任何其他 URL,例如“http:”,也是可能的。

回答by jediz

I ran into similar question (by the title of your question) and managed to instantiate Spring ResourceBundleMessageSourcewith java.util.Properties.

我遇到了类似的问题(你的问题的标题),并设法实例春天ResourceBundleMessageSource会java.util.Properties

ResourceBundleMessageSource messageSource = new ResourceBundleMessageSource();
messageSource.setCommonMessages(properties);

Kind of naive approach, but did the job for my unit testing.

一种天真的方法,但为我的单元测试做了工作。