通过 ConfigurableApplicationContext refresh() 重新加载 spring 应用程序上下文是否被认为是不好的做法 [英] Is spring application context reloading via ConfigurableApplicationContext refresh() considered bad-practice

查看:49
本文介绍了通过 ConfigurableApplicationContext refresh() 重新加载 spring 应用程序上下文是否被认为是不好的做法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们有一个 Spring 应用程序,它托管在一个共享的 tomcat 实例上.

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

有时我们必须重新加载 spring 应用程序上下文,但不想重新启动 Tomcat 服务器,因为其他应用程序也托管在那里.

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.

正在通过

  ((ConfigurableApplicationContext)applicationContext).refresh();

认为不良做法?

我有哪些选择?

推荐答案

一些可能出现的问题 -

A few issues that might arise -

首先,refresh() 应该销毁当前存在于上下文中的所有 bean(单例等)并重新创建它们,因此任何可能发生的引导都会再次发生(您放入 InitializingBean bean 等).这对您来说更像是一个问题,以确保您编写的所有初始化代码都可以安全地再次执行.

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.

另一件需要注意的事情是刷新将如何影响永久内存生成 (permgen).由于 spring 可以(并且将)代理类并创建动态运行时类,这可能被证明是资源泄漏,因为 bean 工厂在刷新上下文时可能会创建新的运行时类.

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.

这篇关于通过 ConfigurableApplicationContext refresh() 重新加载 spring 应用程序上下文是否被认为是不好的做法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆