ReloadableResourceBundleMessageSource与ResourceBundleMessageSource-缓存概念和其他差异 [英] ReloadableResourceBundleMessageSource vs ResourceBundleMessageSource - cache concept & other differences

查看:120
本文介绍了ReloadableResourceBundleMessageSource与ResourceBundleMessageSource-缓存概念和其他差异的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

主应用

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

Beans.xml

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

    </bean>

messages_zh_CN.properties

messages_en_US.properties

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

messages_fr_FR.properties

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要先进得多.

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

1)不限于仅读取.properties文件,还可以读取xml属性文件.

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

2)它不仅限于从类路径中读取文件,而且还可以从任何位置读取文件.

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

"cacheSeconds"的概念是什么

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.

推荐答案

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

Set the number of seconds to cache loaded properties files.

  • 默认值为"-1",表示永远缓存(就像java.util.ResourceBundle一样.)
  • 正数将在给定的秒数内缓存加载的属性文件.这实际上是刷新检查之间的间隔. 请注意,刷新尝试将首先检查文件的最后修改时间戳,然后再实际重新加载它.因此,如果文件没有更改,则可以将此间隔设置得较低,因为刷新尝试实际上不会重新加载.
  • 值"0"将检查每次消息访问时文件的最后修改的时间戳.不要在生产环境中使用它!
  • 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!

请注意,根据ClassLoader的不同,过期可能无法可靠地起作用,因为ClassLoader可能会保留捆绑文件的缓存版本.

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.

在这种情况下,与非类路径位置结合使用,优先使用ReloadableResourceBundleMessageSource而不是ResourceBundleMessageSource.

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

这篇关于ReloadableResourceBundleMessageSource与ResourceBundleMessageSource-缓存概念和其他差异的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆