Interview Questions

Spring Refresh Application Context

In this section, we will discuss about how to reload an application context in Spring.

In many scenarios, you may require to reload the entire application context in Spring. But how to do it? There is actually an easy way to do. Have a look at the following code.

ApplicationContext ctx = new FileSystemXmlApplicationContext("Application-context.xml");
...
// Your application code here
...
((ConfigurableApplicationContext)ctx).refresh();

Take a closer look at the last line, which type casts Application Context in to Configurable Application Context class part and then calls its refresh method. At this point in your application the entire application context (all the beans in Application-context.xml) will be reloaded or recreated.

This is the simple way to reload the context without restarting the web server.