`
MauerSu
  • 浏览: 509606 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

Spring Refresh Application Context

 
阅读更多
源: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.
分享到:
评论

相关推荐

    Spring中的Context你真的懂了吗

    在 refresh 方法中,Spring 会做一些准备工作,如创建 BeanFactory、注册 BeanFactoryPostProcessor 等,只有等这些准备工作做好以后才去开始 Spring Context 的启动。 通过这些内容,我们可以更好地理解 Spring 中...

    Spring的refresh()方法相关异常解析

    Spring refresh()方法相关异常解析 Spring 是一个开放源代码的设计层面框架,它解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spring 是一个轻量级的 Java 开发框架,由...

    testSpring

    信息: Bean factory for application context [org.springframework.context.support.ClassPathXmlApplicationContext@c1f10e]: org.springframework.beans.factory.support.DefaultListableBeanFactory@15e234c ...

    spring容器的触发事件 ClassPathXmlApplicationContext的start()方法的用法

    import org.springframework.context.ApplicationListener; import org.springframework.context.event.ContextClosedEvent; import org.springframework.context.event.ContextRefreshedEvent; import org.spring...

    spring boot中配置mybatis热加载.zip

    import org.springframework.context.annotation.Bean; import org.springframework.core.io.support.PathMatchingResourcePatternResolver; import org.springframework.core.io.support.ResourcePatternResolver; ...

    浅谈Spring Context加载方式

    在Spring框架中,Context是Spring应用的核心容器,它负责管理所有Bean的生命周期和依赖注入。本文将深入探讨Spring Context的两种加载方式:基于XML配置和基于注解的配置。 首先,传统的Spring Context加载方式通常...

    springboot动态配置定时任务(schedule)

    如果需要在不重启的情况下实时更新配置,可以考虑使用Spring Cloud Config Server或使用Spring Boot Actuator的`/refresh`端点来实现配置的热更新。 总结来说,Spring Boot通过`@Scheduled`注解提供了强大的定时...

    Spring在容器在启动的时候发生了什么

    - 初始化bean后处理器和`ApplicationListener`。 - 预初始化一些其他的bean,例如`@PostConstruct`注解的方法会被调用。 - 最后,设置`WebApplicationContext`为“活跃”状态。 这个过程可以通过时序图来形象地表示...

    spring源代码解析

    String ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE = WebApplicationContext.class.getName() + ".ROOT"; …… //对WebApplicationContext来说,需要得到Web容器的ServletContext ServletContext ...

    Spring Boot技术知识点:如何理解@ConfigurationProperties注解

    - 可以通过实现`org.springframework.boot.context.properties.ConfigurationPropertiesBindingPostProcessor`接口来自定义属性绑定逻辑。 10. **配置绑定的生命周期** - 当Spring应用启动时,`@...

    springboot原理解析

    `@SpringBootApplication` 是Spring Boot应用程序的核心注解,它由三个关键注解组成: 1. **@Configuration**:表示这是一个配置类,可以替代XML配置文件。 2. **@EnableAutoConfiguration**:开启自动配置功能,这...

    spring cloud学习教程之config修改配置详解

    在上述代码中,我们使用了`@RefreshScope`注解,这意味着当调用`/refresh`端点时,`Application`类中的`name`属性将被重新加载,从而反映出Git仓库中最新的配置值。 总结一下,Spring Cloud Config 提供了强大的...

    spring Profile如何为不同环境提供不同的配置支持

    context.refresh(); // 刷新上下文 DemoBean demoBean = context.getBean(DemoBean.class); System.out.println(demoBean.getContent()); context.close(); } } ``` ### 2. Spring Boot 中的 Profile 使用 ...

    Spring源代码解析(二):IoC容器在Web容器中的启动.doc

    `ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE`常量用于在`ServletContext`中存储根`WebApplicationContext`的引用,这样Web应用的其他部分就可以找到并使用这个容器。 `XmlWebApplicationContext`是`...

    SpringBoot-2.7.6读取配置文件的方式

    启动应用时,Spring Boot会自动加载`application.properties`或`application.yml`,如果存在的话。另外,你也可以通过`--spring.config.location`命令行参数指定额外的配置文件位置。 ### 2. 配置文件加载顺序 ...

    spring boot环境抽象的实现方法

    context.refresh(); } ``` 3. **默认Profile** 当没有明确指定`profile`时,Spring会使用`default`作为默认`profile`。可以通过`@Profile("default")`注解创建默认配置,当没有其他`profile`被激活时,这个`...

    使用Spring Security OAuth2实现单点登录

    在application.yml文件中,我们需要配置OAuth2客户端的信息: server: port: 8082 servlet: context-path: /ui session: cookie: name: UISESSION security: basic: enabled: false oauth2: client: ...

    ioc容器在Web容器中的启动.pdf

    例如,`ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE`常量用于在`ServletContext`中存储根上下文,使得在整个Web应用程序中可以共享该上下文。 当Web应用程序启动时,Spring通常会使用`XmlWebApplicationContext`作为...

    SpringCloudConfig:配置服务器(使用SpringCloud配置)和充当获取配置的客户端的微服务的示例

    - **Bootstrap Context**:不同于普通的Spring应用上下文,客户端在启动时会首先创建Bootstrap Context来获取配置服务器的地址,然后再创建常规的应用上下文。 - **配置加载**:客户端通过`@RefreshScope`注解实现...

    quartz-demo.zip

    - Spring Boot应用中,可以使用Actuator的 `/shutdown` 和 `/refresh` 端点,分别用于安全地关闭和刷新Quartz调度器。 这个"quartz-demo.zip"中的项目应该包含了上述所有步骤的示例代码,通过查看和运行这些代码,...

Global site tag (gtag.js) - Google Analytics