重新加载使用 setBundle 加载的属性文件
我希望得到一些帮助来解决我在 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以尝试一些事情。
最终将调用ResourceBundle.getBundle(String, Locale, ClassLoader)
,其中该字符串将是您的基本名称,类加载器将是 Thread.currentThread().getContextClassLoader()。如果您使用的是 JDK 1.6,则可以尝试使用ResourceBundle.clearCache(ClassLoader)
清除包缓存。在 servlet 过滤器中执行此操作并将其与其他一些逻辑结合起来以确定何时应清除缓存是有意义的。另一个角度是更直接地控制属性文件的加载和 JSTL 的配置。再次使用过滤器(忽略异常处理):
然后您可以在页面中使用
。您可以在 JSTL API。许多其他变体也是可能的,但请务必查看 JDK 1.6 中较新的
ResourceBundle
(包括ResourceBundle.Control
)添加内容,请记住“的功能”较低级别的”API(例如URLConnection
),并熟悉通过其 API 提供的 JSTL 的更多编程方面。There are a few things you might try.
<fmt:setBundle>
will eventually callResourceBundle.getBundle(String, Locale, ClassLoader)
, where the string will be your basename and the classloader will beThread.currentThread().getContextClassLoader()
. If you're using JDK 1.6, you can try usingResourceBundle.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):
Then you can just use
<fmt:message>
in your pages. You can find the docs forLocalizationContext
andConfig
in the JSTL API.Lots of other variations are possible, but make sure to take a look at the newer
ResourceBundle
(includingResourceBundle.Control
) additions to JDK 1.6, keep in mind the functionality of "lower-level" APIs likeURLConnection
, and become familiar with the more programmatic aspects of JSTL available through its API.谢谢你们的回复。我现在已经成功了,并想分享财富。
因此,我将属性文件从 src 文件夹移到 WEB-INF/properties 中。
我更新了以下 bean 来加载属性文件:
现在,以前我使用 setBundle 来加载到我的属性文件中,如下所示:
但我发现显然我的属性文件不再被加载,因为我已经移动了它。但是由于我的 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:
Now, previously I was using setBundle to load into my properties file like this:
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!
Tomcat 不会重新加载类路径上的资源。这是在 中说明的
ReloadableResourceBundleMessageSource
的 javadoc:此类路径位置包括
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
: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.