ApplicationContext关闭后Spring Bean会发生什么
假设我有一个普通的应用程序,其中我正在使用ApplicationContext ApplicationContext = new FilesystemxmlapplicationContext(“ bean.xml”);
现在,假设在此bean.xml中,一个bean的定义是一个Spring Bean Bean1
,因此当我创建应用程序上下文时,Spring容器将实例化并初始化此bean1
的对象。
然后,我有一个非弹簧对象,对这个弹簧bean的对象进行引用bean1
。
现在,可以说我使用(((configurableApplicationContext)Appctx).close();
关闭应用程序上下文。
现在我的问题是:
- 今年春季bean
bean1
的对象会发生什么,一旦春季的应用程序关闭,它会立即收集垃圾吗? - 由于我在这个弹簧bean
bean1
的对象中持有一个引用,我是否仍然有此对象,或者对object refose condure变量指向此对象将变为null?
Suppose I have a normal application in which I am creating a Spring application context using ApplicationContext applicationContext = new FileSystemXmlApplicationContext("bean.xml");
Now, suppose in this bean.xml there is bean definition for a Spring bean Bean1
, so when I will create the application context then Spring container will instantiate and initialize an object for this Bean1
.
And then I have a non-Spring object holding a reference to the object of this Spring bean Bean1
.
Now, lets say I shutdown the application context using ((ConfigurableApplicationContext)appCtx).close();
.
Now my questions are:
- What will happen to the object this Spring bean
Bean1
, will it be garbage collected as soon as Spring's ApplicationContext is shutdown? - Since I am holding a reference to the object of this Spring bean
Bean1
, will I still have this object or will object reference variable pointing to this will become NULL?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
applicationContext
使用Hashmap
在引擎盖下存储豆子,如果您调用collect
在上面,它将调用clear()
这些hashmap
s上的方法。如您所知,当我们调用
clear()
方法hashmap
上时,它仅分配null
映射gode>条目
如果有另一个对象引用这些对象,它们将生存。因此,您的问题:
bean1
ApplicationContext
usesHashMap
under the hood for storing beans and if you callclose()
on it, its going to callclear()
method on theseHashMap
s.As you know when we call
clear()
method onHashMap
, it's only assigns thenull
to mapEntries
so if there is another object that references to those objects, they will survive.So your questions:
Bean1
看来它不会无效。
我有一个接口教练和实现IT TrackCoach的班级,该方法的方法称为
getDailyWorkout();
我正在使用
getBean()
这是主要类
,输出为以下
It seems that it will not be null.
I have an interface Coach, and class that implements it TrackCoach, which has a method called
getDailyWorkout();
I am doing a basic calling of a bean from the container using
getBean()
Here is a main class
And the output is the following