重新加载使用 setBundle 加载的属性文件

发布于 2024-10-01 05:19:23 字数 1271 浏览 6 评论 0原文

我希望得到一些帮助来解决我在 Spring 中遇到的涉及属性文件的问题。所以我的设置如下:

opto-mapping.properties – 它位于我的 src 文件夹中,包含我的优化资源的翻译,如下所示:

generic-min.css=4037119659.css

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

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

“将属性文件导入到所需的 jsp 中”。然后通过使用引用内容:

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

除了属性文件需要重新启动 tomcat 才能重新加载之外,这一切都工作得很好。我不想每次更新资源时都必须开始关闭网站。我希望属性文件能够经常自动重新加载。

我确实尝试更新 spring-context.xml 中的现有 bean 以重新加载此属性文件,就像我对翻译所做的那样,但这并没有起作用 - 很可能是因为 opto-mapping.properties 文件位置 - 但你看到它需要使用 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>

在这个困难时期,任何帮助或正确方向的指出将不胜感激。

我希望这一切都是有道理的,非常感谢!

G。

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 – 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" />

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

说好的呢 2024-10-08 05:19:25

您可以尝试一些事情。

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

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

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

然后您可以在页面中使用 。您可以在 JSTL API

许多其他变体也是可能的,但请务必查看 JDK 1.6 中较新的 ResourceBundle(包括 ResourceBundle.Control)添加内容,请记住“的功能”较低级别的”API(例如 URLConnection),并熟悉通过其 API 提供的 JSTL 的更多编程方面。

There are a few things you might try.

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

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

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

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.

流云如水 2024-10-08 05:19:24

谢谢你们的回复。我现在已经成功了,并想分享财富。

因此,我将属性文件从 src 文件夹移到 WEB-INF/properties 中。

我更新了以下 bean 来加载属性文件:

<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/properties/opto-mapping</value>
            </list>
        </property>
    </bean>

现在,以前我使用 setBundle 来加载到我的属性文件中,如下所示:

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

但我发现显然我的属性文件不再被加载,因为我已经移动了它。但是由于我的 bean 设置,正在加载新的属性文件,但我的 setBundle 正在覆盖它。

所以,解决方案是删除 setBundle,现在我的属性文件正在重新加载!

再次感谢!

Thank you both for your responses. I have now got this working and thought I would share the wealth.

So, I moved my properties file out of the src folder and into WEB-INF/properties.

I updated the following bean to load up the properties files:

<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/properties/opto-mapping</value>
            </list>
        </property>
    </bean>

Now, previously I was using setBundle to load into my properties file like this:

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

But I found that obviously my properties file wasnt being loaded anymore because I had moved it. But because of my bean setup the new properties file was being loaded but my setBundle was overwritting that.

So, the solution was to remove the setBundle and now my properties file is reloading!

Thanks again!

孤独患者 2024-10-08 05:19:24

Tomcat 不会重新加载类路径上的资源。这是在 中说明的ReloadableResourceBundleMessageSource 的 javadoc

由于应用程序服务器通常会缓存从类路径加载的所有文件,因此有必要将资源存储在其他位置(例如,在 Web 应用程序的“WEB-INF”目录中)。否则类路径中文件的更改将不会反映在应用程序中。

此类路径位置包括WEB-INF/classes,并且不会被释放。

尝试将opto-mapping.properties移动到其他地方(例如WEB-INF/messages),然后再尝试。

Tomcat will not reload resources that are on the classpath. This is stated in the javadoc for ReloadableResourceBundleMessageSource:

Since application servers typically cache all files loaded from the classpath, it is necessary to store resources somewhere else (for example, in the "WEB-INF" directory of a web app). Otherwise changes of files in the classpath will not be reflected in the application.

Such classpath locations include WEB-INF/classes, and will not be released.

Try moving opto-mapping.properties elsewhere (e.g. WEB-INF/messages), and try it then.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文