0% found this document useful (0 votes)
98 views2 pages

Spring Reload

1) The document discusses how to dynamically reload Spring configuration properties without restarting the server. 2) It provides a solution using the spring-reloaded jar which allows properties prefixed with ${ to be reloaded when the properties file changes. 3) The solution involves configuring beans to load the properties file, use the reloading property placeholder, and setup a timer task to check for file changes periodically.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
98 views2 pages

Spring Reload

1) The document discusses how to dynamically reload Spring configuration properties without restarting the server. 2) It provides a solution using the spring-reloaded jar which allows properties prefixed with ${ to be reloaded when the properties file changes. 3) The solution involves configuring beans to load the properties file, use the reloading property placeholder, and setup a timer task to check for file changes periodically.
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 2

Dynamic reloading of properties

Problem:
1) To make spring configuration properties reloadable without server restart 2) 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 restarting container.

Solution: Download following jar file. http://www.wuenschenswert.net/files/spring-reloaded-2007-05-31.zip Extract zip file & modify DEFAULT_RELOADING_PLACEHOLDER_PREFIX property in <spring-reload-home>\src\java\net\wuenschenswert\spring\ReloadingPropertyPlaceholderConfigurer.java From public static final String DEFAULT_RELOADING_PLACEHOLDER_PREFIX = "#{"; to public static final String DEFAULT_RELOADING_PLACEHOLDER_PREFIX = "${";

and recreate jar file., so that properties with prefix ${ will be recognized by spring-reloader.

Example : <bean id="mybean" class="net.wuenschenswert.spring.example.MyBean"> <property name="cachesize" value="${my.cache.size}"/> </bean> Step 2) copy & paste the following three bean declarations to context file where application reading properties like in previous example. Change location property of configproperties bean according to file path. And keep jar file in classpath.

<bean id="configproperties" class="net.wuenschenswert.spring.ReloadablePropertiesFactoryBean"> <property name="location" value="classpath:/net/wuenschenswert/spring/example/config.properties"/> </bean>

<bean id="propertyConfigurer" class="net.wuenschenswert.spring.ReloadingPropertyPlaceholderConfigurer"> <property name="properties" ref="configproperties"/> </bean> <!-- regularly reload property files. --> <bean id="timer" class="org.springframework.scheduling.timer.TimerFactoryBean"> <property name="scheduledTimerTasks"> <bean id="reloadProperties" class="org.springframework.scheduling.timer.ScheduledTimerTask"> <property name="period" value="1000"/> <property name="runnable"> <bean class="net.wuenschenswert.spring.ReloadConfiguration"> <property name="reconfigurableBeans"> <list> <ref bean="configproperties"/> <!-- others... --> </list> </property> </bean> </property> </bean> </property> </bean> Step3) By default spring-reloader will check file changes for every second , we can change period property to value we like.

Reference: http://www.wuenschenswert.net/wunschdenken/archives/127 http://www.wuenschenswert.net/wunschdenken/archives/138

You might also like