我正在尝试使用可重新加载的弹簧资源捆绑包,但春季找不到该文件。我尝试了许多不同的路径,但无法在任何地方工作。在下面的代码中,您会看到我同时加载了来自同一路径变量的弹簧捆绑包和常规捆绑包,但只有一个作品。

我已经对此猛击了很长时间。有人有什么想法吗?

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

消息


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"));
        }
}

有帮助吗?

解决方案

仅作为记录,您可以尝试这种方法:

  1. 用Basename ='Message'声明ReloAdableReSourceBundle bean。
  2. 将此作为参考注入您的豆子
  3. 创建消息。专业文件并将您的东西放入其中
  4. 复制消息。
  5. 躺下来享受

解释:弹簧可重新加载消息捆绑包不使用类路径,因为它可以通过应用程序服务器进行缓存。您可以通过ClassPath:您的保存程序来绕过这一点,但是您不妨再次使用不可重新加载的版本,该版本更快。换句话说,诀窍是使用不会被缓存的WebApp目录。只要让加载程序知道,您就可以将文件放在您的Promoject/Web下方或以下任何地方。

将其放在WebApp-config.xml中

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

这是你的豆子:

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

@Autowired MessageSource ms;}

而且,由于JSF2确实很酷,因此您可能需要在面部上下文中使用它:

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);
}

其他提示

您可以通过在应用程序上下文文件中配置这样的属性文件来指定属性文件不在档案中:

<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>

ReloadableResourceBundleMessageSource 如果您希望它们实际上可以重新保养,则必须将消息文件移出classPath。如果您不在乎可重新加载的功能,则可以将它们放入classpath并使用 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>

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

您可能不应该使用文件阅读,而应使用class路径阅读

Messages.class.getResourceAsStream("/path/from/classpathroot/messages.properties")
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top