Home > Software design >  What happens to Spring bean after ApplicationContext is shutdown
What happens to Spring bean after ApplicationContext is shutdown

Time:06-10

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:

  1. What will happen to the object this Spring bean Bean1, will it be garbage collected as soon as Spring's ApplicationContext is shutdown?
  2. 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?

CodePudding user response:

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

public static void main(String[] args) {
    
    //load spring configuration file
    ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
    // retrieve bean from spring container
    Coach theCoach = context.getBean("myCoach", Coach.class);
    //call methods on bean
    System.out.println(theCoach.getDailyWorkout());
    Object o = new Object();
    o = theCoach;
    System.out.println("this is Object before closing> " o);
    //close context
    System.out.println("Closing context");
    context.close();
    System.out.println("After closing context");
    System.out.println("this is a regular usage of bean reference> " theCoach.getDailyWorkout());
    System.out.println("this is Object AFTER closing> " o);
    Coach c = (Coach) o;
    
    System.out.println("this is AFTER CASTING> " c.getDailyWorkout());

}

And the output is the following

    Jun 09, 2022 6:36:00 PM org.springframework.context.support.AbstractApplicationContext prepareRefresh
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@6d9c638: startup date [Thu Jun 09 18:36:00 CEST 2022]; root of context hierarchy
Jun 09, 2022 6:36:00 PM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
    Run for 20 min 
    this is Object before closing> com.juradent.springdemo.TrackCoach@69b0fd6f
    Closing context
    Jun 09, 2022 6:36:01 PM org.springframework.context.support.AbstractApplicationContext doClose
    INFO: Closing org.springframework.context.support.ClassPathXmlApplicationContext@6d9c638: startup date [Thu Jun 09 18:36:00 CEST 2022]; root of context hierarchy
    After closing context
    this is a regular usage of bean reference> Run for 20 min
    this is Object AFTER closing> com.juradent.springdemo.TrackCoach@69b0fd6f
    this is AFTER CASTING> Run for 20 min

CodePudding user response:

ApplicationContext uses HashMap under the hood for storing beans and if you call close() on it, its going to call clear() method on these HashMaps. 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:

  1. No, Because there is another reference for Bean1
  2. You still have it.
  • Related