ApplicationContext关闭后Spring Bean会发生什么

发布于 2025-02-06 02:38:11 字数 607 浏览 2 评论 0原文

假设我有一个普通的应用程序,其中我正在使用ApplicationContext ApplicationContext = new FilesystemxmlapplicationContext(“ bean.xml”);

现在,假设在此bean.xml中,一个bean的定义是一个Spring Bean Bean1,因此当我创建应用程序上下文时,Spring容器将实例化并初始化此bean1的对象。

然后,我有一个非弹簧对象,对这个弹簧bean的对象进行引用bean1

现在,可以说我使用(((configurableApplicationContext)Appctx).close();关闭应用程序上下文。

现在我的问题是:

  1. 今年春季bean bean1的对象会发生什么,一旦春季的应用程序关闭,它会立即收集垃圾吗?
  2. 由于我在这个弹簧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:

  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?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

自演自醉 2025-02-13 02:38:11

applicationContext使用Hashmap在引擎盖下存储豆子,如果您调用collect在上面,它将调用clear()这些hashmap s上的方法。
如您所知,当我们调用clear()方法hashmap上时,它仅分配null映射gode>条目如果有另一个对象引用这些对象,它们将生存。

因此,您的问题:

  1. 不,因为还有其他提及bean1
  2. 您仍然有。

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 to Bean1
  2. You still have it.
月下伊人醉 2025-02-13 02:38:11

看来它不会无效。

我有一个接口教练和实现IT TrackCoach的班级,该方法的方法称为

getDailyWorkout();

我正在使用getBean()

这是主要类

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());

}

,输出为以下

    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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文