Spring configuration properties: Defaults and Reloading

We’re using the spring framework a lot at work, and it’s generally an enormous improvement over our diverse previous practices for configuration and inter-component relations. However, there are two points where Spring seemed to be a step backwards compared to our previous practice, so I set out to fix them. I hope this will be interesting to others as well – this be my christmas gift…

Default values

Using Spring’s PropertyPlaceholderConfigurer, we separate the configuration of our software into an XML file containing the detailed wiring, and a standard Java Properties file containing simple parameters that can be adjusted by customers (such as cache sizes, TCP/IP port numbers, database connect strings, authentication names and passwords).

Now let’s assume some customer has adapted the properties file, while we ship a new version of our software, which introduces new configuration parameters. The customer shouldn’t have to manually merge and extend his property file; instead, the XML file contains reasonable default values for those properties missing from the customer’s properties file.

In out-of-the-box Spring, there are two ways to do this with a PropertyPlaceholderConfigurer: either put a list of default property bindings into the XML file itself, or put them into another Properties file, which may be contained as a resource in the jar file. Stefan Kleineikenscheidt has a good description of this approach.

What I found somewhat inconvenient is that each property name is listed twice: once in the default values, once when it is assigned to some bean property. So here’s how Stefan’s example would look using my DefaultPropertyPlaceholderConfigurer:

<bean id="propertyConfigurer"
    class="net.wuenschenswert.spring.DefaultPropertyPlaceholderConfigurer">
  <property name="location" value="file:custom/my.properties"/>
</bean>

<bean id="mybean">
  <property name="cachesize" value="${my.cache.size=100}"/>
</bean>

What happens here? The property configurer will load the properties from the given location as usual. Then when it comes to replacing a placeholder, and that placeholder is not defined in the properties file, the default value specified after the equals sign is used!

  ${placeholder=defaultvalue}

Reloading properties

When loading configuration properties from a file, our software would regularly check whether the file’s modification date has changed, and reload the properties if necessary.

I’ll go into this more deeply into the next post… until then, I’ll give you the syntax:

<bean id="configproperties" 
     class="net.wuenschenswert.spring.reconfiguration.ReloadablePropertiesFactoryBean">
  <property name="location" value="file:custom/my.properties"/>
</bean>

<bean id="propertyConfigurer" 
     class="net.wuenschenswert.spring.reconfiguration.ReloadingPropertyPlaceholderConfigurer">
  <property name="properties" ref="configproperties"/>
</bean>

<bean id="mybean">
  <property name="cachesize" value="#{my.cache.size=100}"/>
</bean>

The hash mark identifies the property as reloadable; When the file changes, and the value of the property my.cache.size defined inside that file changes, the setter for the cacheSize property is invoked on the singleton bean named “mybean”. Of course this includes default values, as above.

  #{reloadableplaceholder}
  #{reloadableplaceholder=defaultvalue}

And it actually works!

Haltung

Aus dem Japanischen über das Englische ins Deutsche…

“Wir achten auf Haltung, wenn wir üben… Wenn Deine Haltung schlecht ist, ist Deine Kraft unkoordiniert, und es ist schwer, sie gut zu nutzen oder zu übermitteln. Du verlässt Dich dann z.B. nur noch auf die Kraft Deiner Arme.

Andere Kampfkünste mögen anders sein, aber wir üben, unser Bewusstsein auf den spezifischen Ort des “Zentrums” zu fokussieren, die zentrale Achse. Wenn Du das gut machst, wenn Du entspannt bleibst und die Technik genauso ausführst wie Du es tust wenn Du die Form allein übst, als wäre Dein Partner überhaupt nicht da, dann kannst Du sie ohne Wiederstand ausführen.

Wir benutzen oft die Metapher, dass die Kraft, die wir zu haben glauben, nur die Spitze des Eisbergs ist, aber wenn Du Deinen Körper geschickt benutzt, kannst Du Deinen gesamten Körper benutzen. Das bedeutet eine Haltung der Entspannung, und dann wie Dein Geist zu benutzen ist.”

Tick, Du bist!

The epidemic outbreak has finally reached me… Reinier got me. Fortunately, as with a vaccination, Reinier has reduced some of the symptoms (from 5 down to 3…) So now I’ll have to disclose three (3) things that hardly anyone knows about me, and tag three (3) more bloggers. Here we go.

1. I created my first language when I was six. Ok, to be fair, I had some help from a nerdby friend of my elder brother (or rather, he created it for me and gave it into my custody). Anyway, it had its own letters (syllables consisting of a noun with some consonants on top), its own grammar (featuring five cases and an weird subject-object-predicate sentence order), and some vocabulary. Aside from being completely fascinating at the time, it also gave me a head start when it came to learning real foreign languages, and an awareness of the construction of language.

2. I started programming when I was six, on a Sinclair ZX 80 (predecessor to the better-known ZX 81). It certainly looked like a dummy – when we unpacked it, we wondered: maybe the real computer is below that piece of plastic? as it turned out, the piece of plastic was the computer. It came complete with a BASIC interpreter, graphical block characters and TV output. RAM was so limited that retyping your program every time was no problem at all. Essentially, I watched every keystroke my elder brothers would make when they played with the thing, and later try to do it myself. As a child, you are not wasted yet by that strange affection called “originality” – I was completely content just imitating.

3. Up to the age of about 17, I tried to optimize my physical presence by moving and straining as little as possible. The theory was: moving or standing (instead of sitting) requires energy, and losing energy is bad for you. Fortunately, I got floppy instead of fat (thanks, genes.) Then, somehow, I started slipping into the martial arts world, taking short courses in Gatka, Taek Won Do, Iaido, Capoeira, Tai Chi, Kung Fu, and finally Aikido. Beware, it might happen to you, too.

Exposed enough? So I can pass it on… Mernst, Janhoo, Christian, you’re it!

Theoretically, LISP is powerful

“In theory, practice is just like the theory, but in practice, practice is nothing like the theory. Or so the theory goes.”

“If it quacks like a dove, walks like a bear, and looks like a postbox, it’s not a duck, even if theoretically it still fits the definition of one, for all practical intents and purposes.”

Another extremely funny rant by reinier on the Raganwald blog…

Customer Support How-Not-To

“Hello,

It is the problem of Parallels Desktop for Mac because our product have
no rights to reboot your Mac so just contact your Local Mac Center i
guess you have some hardware problems.

Best regards, […].”

This is an email I received from Parallels customer support upon informing them that my computer crashes and reboots when running Parallels (Apple Hardware Test says everything is OK, no other app crashes).

I am disappointed by their lack of motivation to actually make their product work. To paraphrase: “Your problem is too complex and I’m too much in a hurry so I’ll just blame some other involved party”. (the email only makes sense after inserting a “not” and some commas, so I assume it was written in a hurry – assuming that they can do better, this means that Parallels’ support is either understaffed or unmotivated).

Multimedia-Aikido

So sieht das aus, wenn man Techniken richtig multimedial beschreibt (klick auf die Kamera). Was im virtuellen Dojo WWW natürlich fehlt, ist die Möglichkeit zum Selbermachen und – mit Partner – ausprobieren. Und natürlich die konsistente Linie – Google ist Eklektizismus. Aber weise Worte und was zum Abgucken gibt’s schonmal.

Zielgerichtet Handeln

Bei meiner Aikido-Prüfung wurde eine Technik kritisiert – oberflächlich deswegen, weil ich sie nicht dicht genug am Boden (nicht tief genug) ausgeführt habe.

Heute hatte Ronald eine andere Erklärung. “Da kann man mal sehen, wie wir so denken – wir denken, der Wurf am Ende ist das wichtige, wir konzentrieren uns nur aufs Ziel.” (macht eine luschige Technik vor, indem er sich sofort die Hand schnappt und versucht, die Hand zu werfen)

“Der Wurf ist egal. Die Technik passiert die ganze Zeit vorher.” Kontakt aufnehmen, kleben bleiben, keine Distanz entstehen lassen, immer fühlen, wo der andere gerade ist, führen, “inneren Kontakt herstellen” (Joachim), dranbleiben, das Zentrum des Partners wahrnehmen, sich auf beide Hände gleichzeitig konzentrieren, das eigene Zentrum spüren, hinter seiner Kraft stehen, den anderen herausziehen, sich ganz gemütlich auf dem anderen abstützen, nach aussen schicken. (Meister Tada empfiehlt, man solle bei solchen Techniken “einen süßen Geschmack im Mund haben”. Eine reizvolle Vorstellung, auch wenn man’s selbst nicht nachvollziehen kann…)

Joachim empfiehlt: Tu einfach so lange, als hättest Du’s, bis Du’s irgendwann wirklich hast. Sei dann nicht enttäuscht, dass der andere sich bewegt, obwohl Du “gar nichts” gemacht hast – dann hast Du’s richtig gemacht.

On the leash

“Have you ever seen a little kid taking a big dog for a walk? Pantomimes being pulled around by a giant dog. Don’t let your emotions pull you around like that.”

Shinshinkan, N-sensei

…passt auch dazu, dass Herr Asai gerade eindrucksvoll vorgeführt hat, wie man einen Schüler (Primus?) mit Hilfe eines zweckentfremdeten Gürtels longiert.