重新加载使用setBundle加载的属性文件 [英] Reloading of properties file which is loaded using setBundle

查看:114
本文介绍了重新加载使用setBundle加载的属性文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望对我在Spring中涉及属性文件的问题提供一些帮助。所以我的设置是这样的:

I was hoping for a little help with a problem I am having involving properties files in Spring. So the setup I have is like so:

opto-mapping.properties - 这位于我的src文件夹中,包含我优化的翻译像这样的资源:

opto-mapping.properties – this is located in my src folder and contains translations for my optimised resources like so:

generic-min.css=4037119659.css

每次运行构建优化时,都会更新此属性文件。然后我使用

This properies file is updated every time the build ‘optimise’ is run. I then use

<fmt:setBundle basename="opto-mapping" />

要在我想要的jsp中导入我的属性文件。然后使用以下内容引用内容:

To import my properties file in my desired jsp. Then referencing the content by using:

<fmt:message key='generic-min.css' />

除了属性文件需要重新加载tomcat重启之外,这一切都很好用。我不希望每次更新资源时都要开始关闭网站。我希望属性文件每隔一段时间自动重新加载。

This all works beautifully except that the properties file requires a tomcat restart to be reloaded. I dont want to have to start taking sites down everytime a resource is updated. I would like the properties file to automatically reload every so often.

我确实尝试更新spring-context.xml中的现有bean来重新加载这个属性文件,就像我一样做翻译,但这没有用 - 很可能是因为opto-mapping.properties文件位置 - 但你看到它需要在该位置加载使用fmt:setBundle。

I did attempt to update an existing bean in my spring-context.xml to reload this properties file like I do with translations but this has not worked - more than likely because of the opto-mapping.properties files location - but you see it needs to be in that location to load using fmt:setBundle.

<bean id="messageSource"
        class="org.springframework.context.support.ReloadableResourceBundleMessageSource">
        <property name="cacheSeconds">
            <value>1</value>
        </property>
        <property name="basenames">
            <list>
                <value>WEB-INF/translations/translations</value>
                <value>WEB-INF/classes/opto-mapping</value>
            </list>
        </property>
</bean>

在这困难时期,任何帮助或正确方向的观点都会受到高度赞赏。

Any help or a point in the right direction would be greatly appreciated in this difficult time.

我希望所有这些都能提前做好准备并提前多多谢谢!

I hope all this makes senese and many thanks in advance!

G。

推荐答案

您可以尝试一些事项。

< fmt: setBundle> 最终将调用 ResourceBundle.getBundle(String,Locale,ClassLoader) ,其中字符串将是您的基本名称,类加载器将是 Thread.currentThread()。getContextClassLoader()。如果您使用的是JDK 1.6,可以尝试使用 ResourceBundle.clearCache(ClassLoader) 清除捆绑缓存。在servlet过滤器中执行此操作并将其与其他逻辑组合以确定何时应清除缓存是有意义的。

<fmt:setBundle> will eventually call ResourceBundle.getBundle(String, Locale, ClassLoader), where the string will be your basename and the classloader will be Thread.currentThread().getContextClassLoader(). If you're using JDK 1.6, you can try using ResourceBundle.clearCache(ClassLoader) to clear the bundle cache. It would make sense to do this in a servlet filter and combine it with some other logic to determine when the cache should be cleared.

另一个角度需要更直接的控制加载属性文件和JSTL的配置。再次,使用过滤器(忽略异常处理):

Another angle is take more direct control over the loading of the properties file and the configuration of JSTL. Again, making use of a filter (ignoring exception handling):

ClassLoader ctxLoader = Thread.currentThread().getContextClassLoader();
URL propsURL = ctxLoader.getResource("opto-mapping.properties");
URLConnection propsConn = propsURL.openConnection();
long propsLastModified = propsConn.getLastModified();
// decide if you want to reload...
propsConn.setUseCaches(false);
InputStream propsIn = propsConn.getInputStream();
ResourceBundle propsBundle = new PropertyResourceBundle(propsIn);
propsIn.close();
LocalizationContext propsCtx = new LocalizationContext(propsBundle);
ServletContext servletCtx = this.filterConfig.getServletContext();
Config.set(servletCtx, Config.FMT_LOCALIZATION_CONTEXT, propsCtx);

然后你可以使用< fmt:message> 在您的网页中。您可以在 LocalizationContext 和 Config 的文档。 com / javaee / 5 / jstl / 1.1 / docs / api /rel =nofollow> JSTL API

Then you can just use <fmt:message> in your pages. You can find the docs for LocalizationContext and Config in the JSTL API.

还有很多其他的变化,但请务必查看JDK 1.6中较新的 ResourceBundle (包括 ResourceBundle.Control )新增内容,保留注意低级API的功能,如 URLConnection ,并熟悉通过其API提供的JSTL的更多编程方面。

Lots of other variations are possible, but make sure to take a look at the newer ResourceBundle (including ResourceBundle.Control) additions to JDK 1.6, keep in mind the functionality of "lower-level" APIs like URLConnection, and become familiar with the more programmatic aspects of JSTL available through its API.

这篇关于重新加载使用setBundle加载的属性文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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