- 浏览: 3499721 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
wanglf1207:
EJB的确是个不错的产品,只是因为用起来有点门槛,招来太多人吐 ...
weblogic-ejb-jar.xml的元素解析 -
qwfys200:
总结的不错。
Spring Web Flow 2.0 入门 -
u011577913:
u011577913 写道也能给我发一份翻译文档? 邮件437 ...
Hazelcast 参考文档-4 -
u011577913:
也能给我发一份翻译文档?
Hazelcast 参考文档-4 -
songzj001:
DbUnit入门实战
Spring Properties Reloaded
Some time ago , I promised I would describe how to make spring configuration properties reloadable.
When using a standard spring PropertyPlaceholderConfigurer, properties will be read from a file, and their values can be referenced using a ${…} macro syntax. The expanded property values are usually assigned to bean properties in the xml application context.
Now what I would like to have is this: When the file changes, the properties should be read again, and the updated values should be assigned to the original beans’ properties. The standard Spring answer would be “shut down the application context and launch a new one”, but often we can be much more flexible without disrupting operation (e.g. changing the size of a cache etc.)
I came up with the following design goals:
- Syntax and usage should be as close as possible to the non-reloading standard spring variant
- Not all placeholders should be dynamic - some bean properties cannot meaningfully be changed at runtime, and the configuration should make this appearant.
- The same conversions should take place during initial configuration, and when a property is reloaded
- No extra demon threads - the XML configuration should control when files are checked for updates
- Spring best practices: programming to interfaces, testability.
- Singleton beans are enough - there’s no point in reconfiguring instances of templates.
As an example, let’s take a bean whose “cachesize” property is configured using a placeholder “my.cache.size” taken from a property file “config.properties”. The standard spring way to describe this looks as follows:
<bean id="configproperties" class="org.springframework.beans.factory.config.PropertiesFactoryBean"> <property name="location" value="file:config.properties"/> </bean> <bean id="propertyConfigurer" class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer"> <property name="properties" ref="configproperties"/> </bean> <bean id="mybean" class="net.wuenschenswert.spring.example.MyBean"> <property name="cachesize" value="${my.cache.size}"/> </bean>
The dynamic variant looks very similar, with only some class names changed, and a slightly different placeholder syntax:
<bean id="configproperties" class="net.wuenschenswert.spring.Reloadable PropertiesFactoryBean”> <property name=”location” value=”file:config.properties”/> </bean> <bean id=”propertyConfigurer” class=”net.wuenschenswert.spring.Reloading PropertyPlaceholderConfigurer”> <property name=”properties” ref=”configproperties”/> </bean> <bean id=”mybean” class=”net.wuenschenswert.spring.example.MyBean”> <property name=”cachesize” value=”# {my.cache.size}”/> </bean> <!– regularly reload property files. –> <bean id=”timer” class=”org.springframework.scheduling.timer.TimerFactoryBean”> …(see complete file for details)… <bean>
If you’re interested - just dive into the code , or download the jar . Thanks to my colleagues at coremedia for discussion and insights - the hacking is all mine, so I take the blame.
I used Spring 1.2.8 plus commons-logging for development (don’t know about licensing, so I’ll rather not bundle the binaries). Try running the example, change the config file and observe the log output. Whoa.
java -cp spring-reloaded.jar:spring.jar:commons-logging.jar \\ net.wuenschenswert.spring.example.Main
Implementation
How does it work? In order to get dynamically reloaded properties, we need the following ingredients:
- a factory bean that detects file system changes
- an observer pattern for Properties, so that file system changes can be propagated
- a property placeholder configurer that remembers where which placeholders were used, and updates singleton beans’ properties
- a timer that triggers the regular check for changed files
The observer pattern is implemented by the interfaces and classes ReloadableProperties, ReloadablePropertiesListener, PropertiesReloadedEvent, and ReloadablePropertiesBase. None of them are especially exciting, just normal listener handling. The class DelegatingProperties serves to transparently exchange the current properties when properties are updated. We only update the whole property map at once, so that the application can avoid inconsistent intermediate states (more on this later).
Now the ReloadablePropertiesFactoryBean can be written to create a ReloadableProperties instance (instead of a Properties instance, as the PropertiesFactoryBean does). When prompted to do so, the RPFB checks file modification times, and if necessary, updates its ReloadableProperties. This triggers the observer pattern machinery.
In our case, the only listener is the ReloadingPropertyPlaceholderConfigurer. It behaves just like a standard spring PropertyPlaceholderConfigurer, except that it tracks all usages of placeholders. Now when properties are reloaded, all usages of each modified property are found, and the properties of those singleton beans are assigned again.
If it doesn’t work for you, I’d love to read your comments. If it does, even more so!
发表评论
-
说明SOA监管(SOA Governance)实例(收录备查)
2012-12-19 11:35 1749SOA 已经不是单纯技术问 ... -
Injecting Spring Beans into Java Servlets
2012-11-01 10:21 1933If you are working in a Java ... -
用 HttpServletResponseWrapper 实现 Etag 过滤器
2012-07-09 16:58 3757原文出处:http://blog.chenlb.com/200 ... -
Eclipse Indigo - Cannot install Android ADT Plugin
2012-02-29 01:17 3881When I try to install the And ... -
Eclipse Indigo - Cannot install Android ADT Plugin
2012-02-29 01:13 1985When I try to install the And ... -
[转]mybatis下的分页,支持所有的数据库
2011-07-21 13:21 14835大 家都知道,mybatis的自带分页方法只是逻 ... -
Java framework for text- & console-based forms?
2011-07-21 01:06 1709charva jcurses JNA , ... -
JNA(Java Native Access)学习入门
2011-07-21 01:04 22609Java Native Access 项目 在 ... -
使用IntrospectorCleanupListener 解决quartz引起的内存泄漏
2011-04-20 11:59 13357"在服务器运行过程中,Spring不停的运行的计划任 ... -
DBCP代码研读以及就数据库连接失效的解决
2011-03-31 11:03 3761问题 网上很多评论说DBCP有很多BUG,但是都没有指明是什 ... -
ContextLoaderListener
2010-12-06 15:58 8458(1) org.springframework.web.c ... -
Servlet3.0新功能: 异步处理
2010-12-06 15:22 3175J2EE 6和Glassfish 3V正式发 ... -
Servlet3.0引入的新特性
2010-12-06 15:20 3052Servlet3.0规范的新特性主要是为了3个目的: ... -
100個節點上運行群集亞馬遜EC2上Hazelcast
2010-12-03 23:59 3315本文的目的,適是给妳湮示的細節集群的100個節點。此湮示記錄, ... -
为spring2.5中的jpetstore增加perf4j监控
2010-09-02 13:51 2639perf4j是一款类似于log4j的性能检测工具. 它 ... -
语义网的学习资源大汇集(备忘)
2010-06-23 22:48 1732网上资源 http:/ ... -
使用 JOLAP 实现复杂分析查询
2010-06-06 13:42 1961Shashank Tiwari 在本文中对 ... -
HTML5 Canvas for Internet Explorer
2010-06-04 21:16 1856Canvascape http://www.benjoff ... -
大型网站架构演变和知识体系
2010-06-01 23:47 1956架构演变第一步:物 ... -
Ejb Con Spring
2010-05-20 17:46 2202Contenido [ocultar] 1 ...
相关推荐
此款插件作者也于进去做了升级,但值得高兴的是国外WordPress爱好者,在DB Cache插件的基础上进行了完善,修正的一些BUG,清理了相关代码,发布了 DB Cache Reloaded 插件,此款插件也算是DB Cache升级后的插件吧!...
2.登录wordpress后台控制面板,在plugins中找到WP-Table Reloaded 插件,激活插件后既可使用了插件。 3.在“Tools”中找到“WP-Table Reloaded ”,你会看到如下的几项内容: List Tables:表格列表 列出当前所具备...
Android Loaders _ Reloaded,相关博文:http://www.cnblogs.com/tianzhijiexian/p/4165143.html
标题 "pcsx-reloaded-1-9-93_stm32f407_" 暗示我们正在讨论一个与PCSX Reloaded相关的项目,这是一款著名的PlayStation(PS1)模拟器,用于在个人计算机上运行PS1游戏。这里的"1-9-93"可能是版本号的一部分,而"_stm...
《黑客帝国超酷屏保Matrix Reloaded ScreenSaver 3.2》是一款深受黑客帝国粉丝喜爱的3D屏保程序,它将电影《黑客帝国》中的经典场景与动态视觉效果完美融合,为用户带来沉浸式的桌面体验。这款屏保不仅在视觉上极具...
题目源代码 利用HASH, 希望大家多多沟通
Counter Hack Reloaded, Second Edition: A Step-by-Step Guide to Computer Attacks and Effective Defenses By Ed Skoudis, Tom Liston ISBN: 9780131481046 Publisher: Prentice Hall Prepared for Zhigang Wang,...
《CxBx-Reloaded-QT-GUI:打造Xbox模拟器的新界面探索》 CxBx-Reloaded-QT-GUI项目是一个创新性的尝试,旨在利用QT库为CxBx-Reloaded这款Xbox游戏模拟器开发全新的图形用户界面(GUI)。该项目的发起者希望通过QT的...
Navigation Matrix Reloaded.css
Whether the localization messages should automatically be reloaded 是否国际化信息自动加载 struts.locale The default locale for the Struts application 默认的国际化地区信息 struts.mapper.class ...
**Joomla 2.5 留言板组件 Easybook Reloaded 汉化版** Joomla 是一款基于PHP的开源内容管理系统(CMS),广泛应用于网站的构建和管理。Joomla 2.5 是该系统的一个旧版本,尽管现在已经有更新的版本,但仍有用户依赖...
在Clojure编程环境中,`reloaded.repl`是一个非常有用的工具,它提供了增强的Read-Eval-Print Loop(REPL)工作流程,使得开发者能够更有效地进行代码开发、测试和调试。REPL是Clojure的核心特性之一,允许程序员...
本文讨论了 Bochspwn Reloaded 的检测内核内存泄露的方法,使用 X86仿真和污点跟踪技术。该方法可以检测到操作系统中用户态和内核态之间的通信漏洞,保护系统免受恶意攻击。 用户 ↔ 内核通信pitfalls 在现代操作...
它主要是为黑客游戏而设计的,它是[Reloaded]的完全免费和开源的公共重写,这是我最初的第一个完全成熟的mod loader。 从上自豪地使用C#编程语言的地面而成。 入门 有关更多信息,请访问,这很酷。 会费 强烈鼓励...
在“Tools”中找到“WP-Table Reloaded ”,你会看到如下的几项内容: List Tables:表格列表 列出当前所具备的表格数 Add new Table:添加新表格(如下图所示) Import a Table:导入表格 把相关数据的表格导入到...
"Carry-Stacker-Reloaded: 发薪日2 BLT Mod" 是一个针对流行的第一人称射击游戏《Payday 2》的第三方模组。这个模组是基于BLT(Black Market Lua Toolkit)进行开发的,BLT是为《Payday 2》设计的一个强大的脚本工具...
**WinMobile 虚拟键盘 TouchMe Reloaded 11-03-13** WinMobile,全称为Windows Mobile,是微软开发的一款操作系统,主要用于移动设备,如智能手机和平板电脑。在WinMobile系统中,虚拟键盘是用户进行输入操作的重要...
《pt_protector_reloaded_1_3:网络游戏服务器保护工具详解》 在互联网技术日新月异的今天,网络游戏已经成为众多用户休闲娱乐的重要方式。然而,随着游戏的普及,作弊行为也日益猖獗,严重影响了游戏的公平性和...
《Joomla模块:plg_FlowPlayer.Reloaded-3.0_j15.zip与视频播放功能详解》 在Web开发领域,Joomla作为一个流行的开源内容管理系统(CMS),以其强大的功能和灵活性备受开发者喜爱。本篇文章将深入探讨Joomla的一个...