`
yangzb
  • 浏览: 3499721 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

Spring Properties Reloaded

阅读更多

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!

分享到:
评论

相关推荐

    WordPress数据库缓存插件DB Cache Reloaded

    此款插件作者也于进去做了升级,但值得高兴的是国外WordPress爱好者,在DB Cache插件的基础上进行了完善,修正的一些BUG,清理了相关代码,发布了 DB Cache Reloaded 插件,此款插件也算是DB Cache升级后的插件吧!...

    WP-Table Reloaded 插件

    2.登录wordpress后台控制面板,在plugins中找到WP-Table Reloaded 插件,激活插件后既可使用了插件。 3.在“Tools”中找到“WP-Table Reloaded ”,你会看到如下的几项内容: List Tables:表格列表 列出当前所具备...

    Android Loaders _ Reloaded

    Android Loaders _ Reloaded,相关博文:http://www.cnblogs.com/tianzhijiexian/p/4165143.html

    pcsx-reloaded-1-9-93_stm32f407_

    标题 "pcsx-reloaded-1-9-93_stm32f407_" 暗示我们正在讨论一个与PCSX Reloaded相关的项目,这是一款著名的PlayStation(PS1)模拟器,用于在个人计算机上运行PS1游戏。这里的"1-9-93"可能是版本号的一部分,而"_stm...

    黑客帝国超酷屏保Matrix Reloaded ScreenSaver 3.2

    《黑客帝国超酷屏保Matrix Reloaded ScreenSaver 3.2》是一款深受黑客帝国粉丝喜爱的3D屏保程序,它将电影《黑客帝国》中的经典场景与动态视觉效果完美融合,为用户带来沉浸式的桌面体验。这款屏保不仅在视觉上极具...

    PKU 2939 Flavius Josephus Reloaded

    题目源代码 利用HASH, 希望大家多多沟通

    counter hack reloaded

    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:最初尝试使用QT为CxBx-Reloaded创建新的GUI

    《CxBx-Reloaded-QT-GUI:打造Xbox模拟器的新界面探索》 CxBx-Reloaded-QT-GUI项目是一个创新性的尝试,旨在利用QT库为CxBx-Reloaded这款Xbox游戏模拟器开发全新的图形用户界面(GUI)。该项目的发起者希望通过QT的...

    Navigation Matrix Reloaded.css

    Navigation Matrix Reloaded.css

    Struts课堂笔记.rar--struts2的struts.properties配置文件详解

    Whether the localization messages should automatically be reloaded 是否国际化信息自动加载 struts.locale The default locale for the Struts application 默认的国际化地区信息 struts.mapper.class ...

    Joomla2.5留言板组件Easybook Reloaded汉化版

    **Joomla 2.5 留言板组件 Easybook Reloaded 汉化版** Joomla 是一款基于PHP的开源内容管理系统(CMS),广泛应用于网站的构建和管理。Joomla 2.5 是该系统的一个旧版本,尽管现在已经有更新的版本,但仍有用户依赖...

    reloaded.repl:REPL函数支持重新加载的工作流程

    在Clojure编程环境中,`reloaded.repl`是一个非常有用的工具,它提供了增强的Read-Eval-Print Loop(REPL)工作流程,使得开发者能够更有效地进行代码开发、测试和调试。REPL是Clojure的核心特性之一,允许程序员...

    藏经阁-Bochspwn-Reloaded-Detecting-Kernel-Memory-Disclosure-With-X8

    本文讨论了 Bochspwn Reloaded 的检测内核内存泄露的方法,使用 X86仿真和污点跟踪技术。该方法可以检测到操作系统中用户态和内核态之间的通信漏洞,保护系统免受恶意攻击。 用户 ↔ 内核通信pitfalls 在现代操作...

    Reloaded-II:与X86,X64兼容的下一代通用.NET Core供电的Mod加载程序

    它主要是为黑客游戏而设计的,它是[Reloaded]的完全免费和开源的公共重写,这是我最初的第一个完全成熟的mod loader。 从上自豪地使用C#编程语言的地面而成。 入门 有关更多信息,请访问,这很酷。 会费 强烈鼓励...

    WP-Table Reloaded,WordPress表格插件下载.rar

    在“Tools”中找到“WP-Table Reloaded ”,你会看到如下的几项内容: List Tables:表格列表 列出当前所具备的表格数 Add new Table:添加新表格(如下图所示) Import a Table:导入表格 把相关数据的表格导入到...

    Carry-Stacker-Reloaded:发薪日2 BLT Mod

    "Carry-Stacker-Reloaded: 发薪日2 BLT Mod" 是一个针对流行的第一人称射击游戏《Payday 2》的第三方模组。这个模组是基于BLT(Black Market Lua Toolkit)进行开发的,BLT是为《Payday 2》设计的一个强大的脚本工具...

    WinMobile 的虚拟键盘 TouchMe Reloaded 11-03-13

    **WinMobile 虚拟键盘 TouchMe Reloaded 11-03-13** WinMobile,全称为Windows Mobile,是微软开发的一款操作系统,主要用于移动设备,如智能手机和平板电脑。在WinMobile系统中,虚拟键盘是用户进行输入操作的重要...

    pt_protector_reloaded_1_3

    《pt_protector_reloaded_1_3:网络游戏服务器保护工具详解》 在互联网技术日新月异的今天,网络游戏已经成为众多用户休闲娱乐的重要方式。然而,随着游戏的普及,作弊行为也日益猖獗,严重影响了游戏的公平性和...

    plg_FlowPlayer.Reloaded-3.0_j15.zip

    《Joomla模块:plg_FlowPlayer.Reloaded-3.0_j15.zip与视频播放功能详解》 在Web开发领域,Joomla作为一个流行的开源内容管理系统(CMS),以其强大的功能和灵活性备受开发者喜爱。本篇文章将深入探讨Joomla的一个...

Global site tag (gtag.js) - Google Analytics