Spring’s refreshable beans

A couple of days ago I found out about a really nice feature in Spring, called ‘refreshable bean’.

Spring’s vision a refreshable bean is a dynamic-language-backed bean that monitors changes to its source code and then reloads itself when changes occur. And it is all this is done without restarting/re-deploying entire app. Sweet!

Currently Spring supports only 3 dynamic languages: JRuby, Groovy, BeanShell.

A simple example taken from springsource’s website looks like:

    <lang:groovy id="messenger"
          <!-- switches refreshing on with 5 seconds between checks -->
        <lang:property name="message" value="I Can Do The Frug" />
    <bean id="bookingService" class="x.y.DefaultBookingService">
        <property name="messenger" ref="messenger" />

So, the ‘refresh-check-delay’ attribute makes ‘messenger’ bean ‘refreshable’. This attribute is the number of milliseconds between checks to the bean’s source code, if it has changed, the bean will be refreshed.

Another thing to mention would be that new changes are effective only on the bean’s next usage. For example if you make five changes to the beans source code, but never make a call to the bean between changes, it will only be reloaded once, when you actually make a call to the bean.

Also, refreshable beans behavior does not apply to dynamic language source code defined inline, inside the context file, it only applies to beans where changes to source code file can be detected, for example if it’s stored on the filesystem.

That’s about it. You can find more on springsource’s website: Dynamic language