找不到属性文件 ReloadableResourceBundleMessageSource 错误
我正在尝试使用可重新加载的 spring 资源包,但 spring 找不到该文件。我尝试了很多不同的路径,但无法让它在任何地方工作。在下面的代码中,您将看到我从同一路径变量加载了 spring 包和常规包,但只有一个有效。
我已经对这个问题头疼了太久了。有人有什么想法吗?
日志
INFO 2010-04-28 11:38:31,805 [main] org.myorg.test.TestMessages: C:\www\htdocs\messages.properties
INFO 2010-04-28 11:38:31,805 [main] org.myorg.data.Messages: initializing Spring Message Source to C:\www\htdocs\messages.properties
INFO 2010-04-28 11:38:31,821 [main] org.myorg.data.Messages: Attempting to load properties from C:\www\htdocs\messages.properties
DEBUG 2010-04-28 11:38:31,836 [main] org.springframework.context.support.ReloadableResourceBundleMessageSource: No properties file found for [C:\www\htdocs\messages.properties_en_US] - neither plain properties nor XML
DEBUG 2010-04-28 11:38:31,842 [main] org.springframework.context.support.ReloadableResourceBundleMessageSource: No properties file found for [C:\www\htdocs\messages.properties_en] - neither plain properties nor XML
DEBUG 2010-04-28 11:38:31,848 [main] org.springframework.context.support.ReloadableResourceBundleMessageSource: No properties file found for [C:\www\htdocs\messages.properties] - neither plain properties nor XML
INFO 2010-04-28 11:38:31,848 [main] org.myorg.test.TestMessages: I am C:\www\htdocs\messages.properties
文件 Messages.java
package org.myorg.data;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
public class Messages {
protected static final Log logger = LogFactory.getLog(Messages.class);
private static ReloadableResourceBundleMessageSource msgSource = null;
private static ResourceBundle RESOURCE_BUNDLE;
public static final String PATH = "C:" + File.separator + "www"
+ File.separator + "htdocs" + File.separator + "messages.properties";
private Messages() {
}
public static String getString(String key) {
initBundle();
return msgSource.getMessage(key, null,
RESOURCE_BUNDLE.getString(key), null);
}
private static void initBundle(){
if(null == msgSource || null == RESOURCE_BUNDLE){
logger.info("initializing Spring Message Source to " + PATH);
msgSource = new ReloadableResourceBundleMessageSource();
msgSource.setBasename(PATH);
msgSource.setCacheSeconds(1);
FileInputStream fis = null;
try {
logger.info("Attempting to load properties from " + PATH);
fis = new FileInputStream(PATH);
RESOURCE_BUNDLE = new PropertyResourceBundle(fis);
} catch (Exception e) {
logger.info("couldn't find " + PATH);
} finally {
try {
if(null != fis)
fis.close();
} catch (IOException e) {
}
}
}
}
}
TestMessages.java
package org.myorg.test;
import org.myorg.data.Messages;
public class TestMessages extends AbstractTest {
public void testMessage(){
logger.info(Messages.PATH);
logger.info(Messages.getString("OpenKey.TEST"));
}
}
I'm trying to use a reloadable spring resource bundle but spring cannot find the file. I've tried tons of different paths, but can't get it to work anywhere. In the code below you'll see that i load both the spring bundle and the regular one from the same path variable but only one works.
I've been banging my head against this for far too long. Anybody have any ideas?
logfile
INFO 2010-04-28 11:38:31,805 [main] org.myorg.test.TestMessages: C:\www\htdocs\messages.properties
INFO 2010-04-28 11:38:31,805 [main] org.myorg.data.Messages: initializing Spring Message Source to C:\www\htdocs\messages.properties
INFO 2010-04-28 11:38:31,821 [main] org.myorg.data.Messages: Attempting to load properties from C:\www\htdocs\messages.properties
DEBUG 2010-04-28 11:38:31,836 [main] org.springframework.context.support.ReloadableResourceBundleMessageSource: No properties file found for [C:\www\htdocs\messages.properties_en_US] - neither plain properties nor XML
DEBUG 2010-04-28 11:38:31,842 [main] org.springframework.context.support.ReloadableResourceBundleMessageSource: No properties file found for [C:\www\htdocs\messages.properties_en] - neither plain properties nor XML
DEBUG 2010-04-28 11:38:31,848 [main] org.springframework.context.support.ReloadableResourceBundleMessageSource: No properties file found for [C:\www\htdocs\messages.properties] - neither plain properties nor XML
INFO 2010-04-28 11:38:31,848 [main] org.myorg.test.TestMessages: I am C:\www\htdocs\messages.properties
Messages.java
package org.myorg.data;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.util.PropertyResourceBundle;
import java.util.ResourceBundle;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.context.support.ReloadableResourceBundleMessageSource;
public class Messages {
protected static final Log logger = LogFactory.getLog(Messages.class);
private static ReloadableResourceBundleMessageSource msgSource = null;
private static ResourceBundle RESOURCE_BUNDLE;
public static final String PATH = "C:" + File.separator + "www"
+ File.separator + "htdocs" + File.separator + "messages.properties";
private Messages() {
}
public static String getString(String key) {
initBundle();
return msgSource.getMessage(key, null,
RESOURCE_BUNDLE.getString(key), null);
}
private static void initBundle(){
if(null == msgSource || null == RESOURCE_BUNDLE){
logger.info("initializing Spring Message Source to " + PATH);
msgSource = new ReloadableResourceBundleMessageSource();
msgSource.setBasename(PATH);
msgSource.setCacheSeconds(1);
FileInputStream fis = null;
try {
logger.info("Attempting to load properties from " + PATH);
fis = new FileInputStream(PATH);
RESOURCE_BUNDLE = new PropertyResourceBundle(fis);
} catch (Exception e) {
logger.info("couldn't find " + PATH);
} finally {
try {
if(null != fis)
fis.close();
} catch (IOException e) {
}
}
}
}
}
TestMessages.java
package org.myorg.test;
import org.myorg.data.Messages;
public class TestMessages extends AbstractTest {
public void testMessage(){
logger.info(Messages.PATH);
logger.info(Messages.getString("OpenKey.TEST"));
}
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
只是为了记录,您可以尝试这种方法:
说明:
Spring 可重新加载消息包不使用类路径,因为它由应用服务器缓存。您可以通过 classpath:your.properies 来规避此问题,但您也可以使用不可重新加载的版本,该版本的速度要快一些。
换句话说,技巧是使用不被缓存的 webApp 目录。您可以将文件放在 yourProject/web 下或下面的任何位置,只要您让加载程序知道即可。
将其放入 webapp-config.xml 中
,这就是您的 bean:
由于 JSF2 确实很酷,您可能想在 faces 上下文中使用它:
Just for the record you may try this approach:
Explanation:
Spring reloadable message bundle doesn't use class path as it get's cached by app servers. You may circumvent this by classpath:your.properies but then again you may as well use the non reloadable version which is a few ticks faster.
In other words the trick is to use the webApp directory which doesn't get cached. You may place your files under yourProject/web or anywhere below as long as you let the loader know.
place this in webapp-config.xml
and this is your bean:
And since JSF2 is really cool stuff you may want to use this in a faces context then:
使用ReloadableResourceBundleMessageSource,如果您希望消息文件真正可重新加载,则必须将消息文件移出类路径。如果您不关心可重新加载功能,您可以将它们放入类路径中并使用
classpath:
前缀。我只是切换到
ReloadableResourceBundleMessageSource
。这对我有用。With
ReloadableResourceBundleMessageSource
you have to move the message files out of the classpath if you want them to actually be reloadable. If you don't care for the reloadable feature you can put them in the classpath and use theclasspath:
prefix.I just switch to
ReloadableResourceBundleMessageSource
. This is working for me.您可以通过在应用程序上下文文件中进行配置来将属性文件指定在存档之外,如下所示:
You can specify the properties file(s) be outside of the archive by configuring in the application context file like so:
您可能应该使用从类路径中读取,而不是从文件中读取
rather than reading from a file, you should probably read from the classpath using