2

我正在学习春天。我尝试在 ResourceBundleMessageSource 下使用,这是我尝试的示例。

主应用

public class MainApp {

    public static void main(String arg[]){
        ApplicationContext context = new ClassPathXmlApplicationContext("Beans.xml");



        String text = context.getMessage("s.wish",
                new Object[] {"saro", "stanes" },
                                        Locale.ENGLISH);

        System.out.println("English... " + text);

        String text2 = context.getMessage("s.wish",
                new Object[] {"saro", "stanes" },
                                        Locale.FRANCE);

        System.out.println("French... " + text2);
    }
}

豆类.xml

<!-- resource bundle -->
     <bean id="messageSource"
        class="org.springframework.context.support.ResourceBundleMessageSource ">
        <property name="basename" value="resources/locale/messages"/>

    </bean>

messages_en_US.properties

s.wish=good morning, name : {0}, school : {1}

messages_fr_FR.properties

s.wish=bonjour, name : {0}, school : {1}

输出:

English... good morning, name : saro, school : stanes
French... bonjour, name : saro, school : stanes

从文档中我了解到 ReloadableResourceBundleMessageSource 比 ResourceBundleMessageSource 先进得多。

1)不限于单独读取.properties文件,也可以读取xml属性文件。

2)它不仅限于从类路径读取文件,而是从任何位置读取文件。

“cacheSeconds”的概念是什么

class="org.springframework.context.support.ReloadableResourceBundleMessageSource ">
        <property name="basename" value="resources/locale/messages"/>
        <property name="cacheSeconds" value="3600"/>
    </bean> 

任何人都可以简要介绍一下或帮助我举一个例子来更好地理解。

4

1 回答 1

3

设置缓存加载的属性文件的秒数。

  • 默认为“-1”,表示永久缓存(就像 java.util.ResourceBundle 一样)。
  • 正数将在给定的秒数内缓存加载的属性文件。这本质上是刷新检查之间的间隔。请注意,刷新尝试将在实际重新加载文件之前首先检查文件的最后修改时间戳;所以如果文件没有改变,这个间隔可以设置得相当低,因为刷新尝试实际上不会重新加载。
  • “0”值将在每次消息访问时检查文件的最后修改时间戳。不要在生产环境中使用它!

请注意,根据您的 ClassLoader,到期可能无法可靠地工作,因为 ClassLoader 可能会保留捆绑文件的缓存版本。

在这种情况下,首选 ReloadableResourceBundleMessageSource 而不是 ResourceBundleMessageSource,并结合非类路径位置。

于 2017-04-24T15:52:35.867 回答