ReloadableResourceBundleMessageSource vs ResourceBundleMessageSource - cache concept & other differences

只愿长相守 提交于 2020-01-04 11:03:01

问题


I am learning spring. I tried to under the use of ResourceBundleMessageSource and here is the example I tried.

Main App

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

Beans.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}

output:

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

From the docs I understand ReloadableResourceBundleMessageSource is way more advance than ResourceBundleMessageSource.

1) It is not restricted to read .properties files alone but can read xml property files as well.

2) It is not restricted to reading files from just classpath but from any location.

What is the concept around "cacheSeconds"

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

Could anyone brief on that or help me with an example to understand better.


回答1:


Set the number of seconds to cache loaded properties files.

  • Default is "-1", indicating to cache forever (just like java.util.ResourceBundle).
  • A positive number will cache loaded properties files for the given number of seconds. This is essentially the interval between refresh checks. Note that a refresh attempt will first check the last-modified timestamp of the file before actually reloading it; so if files don't change, this interval can be set rather low, as refresh attempts will not actually reload.
  • A value of "0" will check the last-modified timestamp of the file on every message access. Do not use this in a production environment!

Note that depending on your ClassLoader, expiration might not work reliably since the ClassLoader may hold on to a cached version of the bundle file.

Prefer ReloadableResourceBundleMessageSource over ResourceBundleMessageSource in such a scenario, in combination with a non-classpath location.



来源:https://stackoverflow.com/questions/39685399/reloadableresourcebundlemessagesource-vs-resourcebundlemessagesource-cache-con

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!