欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

Spring Refresh Application Context

程序员文章站 2022-07-14 10:26:19
...
源:http://techdive.in/spring/spring-refresh-application-context
评:
Submitted by arunraj on Sun, 07/18/2010 - 14:59


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.

-------------
源:http://www.4byte.cn/question/1884180/is-spring-application-context-reloading-via-configurableapplicationcontext-refresh-considered-bad-practice.html
评:

We have a Spring application that is hosted on a shared tomcat instance.

Sometimes we have to reload the spring application context but don't want to restart the Tomcat server, because other application's are hosted there as well.

Is refreshing springs application context via

  ((ConfigurableApplicationContext)applicationContext).refresh();
considered bad practice?

What alternatives do I have?


Best Answer


A few issues that might arise -

Firstly, refresh() should destroy all beans currently living in the context (singletons etc) and recreate them, so any bootstrapping that might happen will happen again (stuff you put in InitializingBean beans etc). This is more of an issue for you, to make sure that all initializing code you've written is safe to be executed again.

Another thing to keep an eye on is how a refresh will affect the permanent memory generation (permgen). Since spring can (and will) proxy classes and create on-the-fly runtime-classes, this may prove to be a resource-leak since the bean-factory will probably create new runtime-classes when it refreshed the context.