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
External properties file as spring MessageSource not working
提问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/resource
and bundles/override
get fetched when I call busmessageSource.getMessage("anykey", null, null)
这里的属性来自bundles/resource
并bundles/override
在我调用时获取busmessageSource.getMessage("anykey", null, null)
but it fails when I try to fetch values for properties in C:/mmt/override
但是当我尝试为 中的属性获取值时它失败了 C:/mmt/override
- What is correct way of configuring messagesource with external file from the disk.
- Also I want
file:/C:/mmt/override
to override values inclasspath:bundles/override
if any with the same key exist. How do I override properties from an external file outside of my war folder?
- 使用磁盘中的外部文件配置消息源的正确方法是什么。
- 如果存在任何具有相同键的
file:/C:/mmt/override
值,我也想覆盖其中的值classpath:bundles/override
。如何从我的 war 文件夹之外的外部文件覆盖属性?
回答by Armando
1.)I have these 3 ways:
1.)我有这 3 种方式:
- One solution is to add your "
C:/mmt/
" folder to your resource classpath. - This is another way that may help you ResourceBundle not found for MessageSource when placed inside a folder
- Use this code: (Worked for me)
- 一种解决方案是将“
C:/mmt/
”文件夹添加到资源类路径中。 - 这是另一种方法,可以帮助您将ResourceBundle 放置在文件夹中时找不到 MessageSource
- 使用此代码:(为我工作)
<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 ReloadableResourceBundleMessageSource
class.
注意 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:
关于消息资源请注意:
- A plain path will be relative to the current application context.
- A "classpath:" URL will be treated as classpath resource.
- A "file:" URL will load from an absolute file system path.
- Any other URL, such as "http:", is possible too.
- 普通路径将相对于当前应用程序上下文。
- “classpath:” URL 将被视为类路径资源。
- “file:” URL 将从绝对文件系统路径加载。
- 任何其他 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.
一种天真的方法,但为我的单元测试做了工作。