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.
评:
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.
推荐阅读
-
How to add OpenCV library in spring web application
-
浅谈Spring Context加载方式
-
Spring的refresh()方法相关异常解析
-
Spring Boot中配置文件application.properties使用
-
关于Spring启动时Context加载源码分析
-
浅谈Spring Context加载方式
-
Spring Boot中配置文件application.properties使用
-
Spring Boot 中application.yml与bootstrap.yml的区别
-
关于Spring启动时Context加载源码分析
-
spring boot application properties配置实例代码详解