Issue
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?
Solution
ApplicationContext
uses HashMap
under the hood for storing beans and if you call close()
on it, its going to call clear()
method on these HashMap
s.
As you know when we call clear()
method on HashMap
, it's only assigns the null
to map Entries
so if there is another object that references to those objects, they will survive.
So your questions:
- No, Because there is another reference to
Bean1
- You still have it.
Answered By - hatef alipoor
Answer Checked By - Timothy Miller (JavaFixing Admin)