java 找不到属性文件 ReloadableResourceBundleMessageSource 错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2732280/
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-10-29 22:35:28  来源:igfitidea点击:

No properties file found Error for ReloadableResourceBundleMessageSource

javaspringresourcebundle

提问by samspot

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.

我正在尝试使用可重新加载的 spring 资源包,但 spring 找不到该文件。我已经尝试了很多不同的路径,但不能让它在任何地方工作。在下面的代码中,您将看到我从同一路径变量加载了 spring 包和常规包,但只有一个有效。

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

消息.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

测试消息.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"));
        }
}

回答by Steven Varga

Just for the record you may try this approach:

只是为了记录,您可以尝试这种方法:

  1. declare the reloadableResourceBundle bean with basename='messages' .properties extension is assumed and not required
  2. inject this as reference into your bean
  3. create messages.properties file and place your stuff in it
  4. copy messages.properties into yourProject/web/messages.properties
  5. lay back and enjoy
  1. 使用 basename='messages' .properties 扩展名声明 reloadableResourceBundle bean 是假定的而不是必需的
  2. 将此作为参考注入到您的 bean 中
  3. 创建messages.properties文件并将您的东西放入其中
  4. 将 messages.properties 复制到 yourProject/web/messages.properties
  5. 躺下来享受

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.

说明:Spring 可重新加载的消息包不使用类路径,因为它被应用服务器缓存。您可以通过 classpath:your.properies 来规避这一点,但您也可以使用不可重新加载的版本,它的速度要快一些。换句话说,诀窍是使用不会被缓存的 webApp 目录。只要让加载程序知道,您就可以将文件放在 yourProject/web 下或下面的任何位置。

place this in webapp-config.xml

把它放在 webapp-config.xml 中

<bean id="messageSource"  class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="basename" value="message"/>
    <property name="cacheSeconds" value="1"/>
</bean>

and this is your bean:

这是你的豆子:

public class YouBeanWithMsgReloadableResourceBundle {
    public void yourMethod(){
    String msg = ms.getMessage("your.memo.nic", null, "your default message", Locale.CANADA);
}

@Autowired MessageSource ms;}

And since JSF2 is really cool stuff you may want to use this in a faces context then:

由于 JSF2 是非常酷的东西,你可能想在 faces 上下文中使用它,那么:

public void validate(FacesContext context, UIComponent component, Object value)
        throws ValidatorException {
MessageSource ms = FacesContextUtils.getWebApplicationContext(context).getBean(MessageSource.class);
String msg = ms.getMessage("your.memo.nic", null, "your default message", Locale.CANADA);
}

回答by MikeNereson

With ReloadableResourceBundleMessageSourceyou 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 the classpath:prefix.

有了ReloadableResourceBundleMessageSource你,如果你想他们实际上是重新加载到消息文件移出类路径。如果您不关心可重新加载的功能,您可以将它们放在类路径中并使用classpath:前缀。

<bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="basenames">
        <list>
            <value>classpath:messages/admin_i18n</value>
            <value>classpath:messages/customer_i18n</value>
        </list>
    </property>
</bean>

I just switch to ReloadableResourceBundleMessageSource. This is working for me.

我只是切换到ReloadableResourceBundleMessageSource. 这对我有用。

回答by Tommy Stephenson

You can specify the properties file(s) be outside of the archive by configuring in the application context file like so:

您可以通过在应用程序上下文文件中进行配置来指定存档之外的属性文件,如下所示:

<bean id="messageSource"
      class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
    <property name="defaultEncoding" value="UTF-8"/>
    <property name="basenames">
        <list>
            <value>file:/whatever_your_file_path_is/messages/admin_i18n</value>
            <value>file:/whatever_your_file_path_is/messages/customer_i18n</value>
        </list>
    </property>
</bean>

回答by MeBigFatGuy

rather than reading from a file, you should probably read from the classpath using

而不是从文件中读取,您应该使用从类路径中读取

Messages.class.getResourceAsStream("/path/from/classpathroot/messages.properties")