Reload / update Spring configuration file without restarting servlet container

How to update Spring configuration file without reloading servlet container?

I am looking for a solution other than JRebel.

+38
spring
Feb 10 '09 at 20:23
source share
4 answers

Well, it may be useful to perform such a context reload when testing your application.

You can try the refresh method of one of the AbstractRefreshableApplicationContext classes: it will not update your previously installed beans, but the next context call will return the updated beans.

 import java.io.File; import java.io.IOException; import org.apache.commons.io.FileUtils; import org.springframework.context.support.FileSystemXmlApplicationContext; public class ReloadSpringContext { final static String header = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + "<!DOCTYPE beans PUBLIC \"-//SPRING//DTD BEAN//EN\"\n" + " \t\"http://www.springframework.org/dtd/spring-beans.dtd\">\n"; final static String contextA = "<beans><bean id=\"test\" class=\"java.lang.String\">\n" + "\t\t<constructor-arg value=\"fromContextA\"/>\n" + "</bean></beans>"; final static String contextB = "<beans><bean id=\"test\" class=\"java.lang.String\">\n" + "\t\t<constructor-arg value=\"fromContextB\"/>\n" + "</bean></beans>"; public static void main(String[] args) throws IOException { //create a single context file final File contextFile = File.createTempFile("testSpringContext", ".xml"); //write the first context into it FileUtils.writeStringToFile(contextFile, header + contextA); //create a spring context FileSystemXmlApplicationContext context = new FileSystemXmlApplicationContext( new String[]{contextFile.getPath()} ); //echo the bean 'test' on stdout System.out.println(context.getBean("test")); //write the second context into it FileUtils.writeStringToFile(contextFile, header + contextB); //refresh the context context.refresh(); //echo the bean 'test' on stdout System.out.println(context.getBean("test")); } } 

And you will get this result

 fromContextA fromContextB 

Another way to achieve this (and possibly simpler) is to use the Refreshable Bean Spring 2.5+ function With a dynamic language (groovy, etc.) And Spring you can even change the Bean behavior. Take a look at the spring link for a dynamic language :

24.3.1.2. Updated beans

One of (if not the most) irresistible incremental values โ€‹โ€‹of dynamic language support in Spring is the 'refreshable bean'.

The updated Bean is with support for the dynamic Bean language, which with a small configuration, with support for the dynamic Bean language, track changes to the source file resource, and then reload when the dynamic language changes the source file (for example, when the developer edits and saves the changes in the file to the File system).

+11
Jan 22 '10 at 9:24
source share

For those who stumbled upon this very recently, a relevant and modern way to solve this problem is to use Spring Boot Cloud Configuration .

Just add the @RefreshScope annotation to your updatable beans and @EnableConfigServer in your main / configuration.

So, for example, this controller class:

 @RefreshScope @RestController class MessageRestController { @Value("${message}") private String message; @RequestMapping("/message") String getMessage() { return this.message; } } 

The new value of your message String property for the /message endpoint will be returned when you update your configuration.

See the official Spring Central Configuration Guide for more information.

+9
Nov 28 '17 at 19:03
source share

I would not recommend you to do this. What do you expect from a singleton beans that changed their configuration? do you expect all singletones to reboot? but some objects may contain links to these singletones.

See this post, as well as Automatic Re-Initialization of Configuration in Spring

+7
Feb 10 '09 at 22:22
source share

You can look at http://www.wuenschenswert.net/wunschdenken/archives/138 , where as soon as you change any thing in the properties file and save it, beans will reload with new values.

+2
Apr 30 2018-12-12T00:
source share



All Articles