Spring Singleton Scope如何收集垃圾?

时间:2022-06-29 23:58:23

I am new to Spring framework. I have been confused about the concept of singleton in Spring and it's garbage collection. I have read many questions and articles to get the answer to my question that how Spring Singleton scope is garbage collected. I only got the answers about prototype scope garbage collection, but the articles regarding singleton scope were not clear to me. Could someone give the details on this issue.

我是Spring框架的新手。我对Spring中的singleton概念感到困惑,它是垃圾收集。我已经阅读了很多问题和文章来获得我的问题的答案,即Spring Singleton范围是如何被垃圾收集的。我只得到了关于原型范围垃圾收集的答案,但关于单例范围的文章对我来说并不清楚。有人可以提供有关此问题的详细信息。

1 个解决方案

#1


6  

In Spring, most of the classes you write will be Singletons. This means that there is only ever one instance of these classes created. These classes are created when the Spring container starts and are destroyed when the Spring container stops.

在Spring,你写的大多数课程都是单身。这意味着只创建了这些类的一个实例。这些类是在Spring容器启动时创建的,并在Spring容器停止时被销毁。

The reason that Spring singleton objects are different from simple Java objects, is that the container maintains a reference to them, and they are able to be used anywhere in your code at any time.

Spring单例对象与简单Java对象的不同之处在于容器维护对它们的引用,并且它们可以随时在代码中的任何位置使用。

I'll give you an example using the Spring container to illustrate what I mean. This is NOT how you should do this normally when writing a Spring app, this is just an example.

我将举例说明使用Spring容器来说明我的意思。在编写Spring应用程序时,这不是您应该如何正常执行此操作,这只是一个示例。

@Component
public class ExampleClass implements ApplicationContextAware {
    /* 
     * The ApplicationContextAware interface is a special interface that allows 
     * a class to hook into Spring's Application Context. It should not be used all
     * over the place, because Spring provides better ways to get at your beans
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        MyBean bean = applicationContext.getBean("MyBean");
    }
}

What the above code does is say to Spring "I want the instance of MyBean that you have discovered when the container started" (Classpath Scanning). Spring should have a (proxy) instance of this class already created and available for your use.

以上代码的作用是对Spring说“我想要在容器启动时发现的MyBean实例”(Classpath Scanning)。 Spring应该已经创建了这个类的(代理)实例,可供您使用。

From the Spring Documentation

来自Spring文档

The Spring IoC container creates exactly one instance of the object defined by that bean definition. This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object.

Spring IoC容器只创建该bean定义定义的对象的一个​​实例。此单个实例存储在此类单例bean的缓存中,并且该命名Bean的所有后续请求和引用都将返回缓存对象。

Because that bean has been cached inside the application context, it is never eligible for garbage collection until the application context is destroyed.

因为该bean已缓存在应用程序上下文中,所以在销毁应用程序上下文之前,它永远不会有资格进行垃圾回收。

#1


6  

In Spring, most of the classes you write will be Singletons. This means that there is only ever one instance of these classes created. These classes are created when the Spring container starts and are destroyed when the Spring container stops.

在Spring,你写的大多数课程都是单身。这意味着只创建了这些类的一个实例。这些类是在Spring容器启动时创建的,并在Spring容器停止时被销毁。

The reason that Spring singleton objects are different from simple Java objects, is that the container maintains a reference to them, and they are able to be used anywhere in your code at any time.

Spring单例对象与简单Java对象的不同之处在于容器维护对它们的引用,并且它们可以随时在代码中的任何位置使用。

I'll give you an example using the Spring container to illustrate what I mean. This is NOT how you should do this normally when writing a Spring app, this is just an example.

我将举例说明使用Spring容器来说明我的意思。在编写Spring应用程序时,这不是您应该如何正常执行此操作,这只是一个示例。

@Component
public class ExampleClass implements ApplicationContextAware {
    /* 
     * The ApplicationContextAware interface is a special interface that allows 
     * a class to hook into Spring's Application Context. It should not be used all
     * over the place, because Spring provides better ways to get at your beans
     */
    @Override
    public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
        MyBean bean = applicationContext.getBean("MyBean");
    }
}

What the above code does is say to Spring "I want the instance of MyBean that you have discovered when the container started" (Classpath Scanning). Spring should have a (proxy) instance of this class already created and available for your use.

以上代码的作用是对Spring说“我想要在容器启动时发现的MyBean实例”(Classpath Scanning)。 Spring应该已经创建了这个类的(代理)实例,可供您使用。

From the Spring Documentation

来自Spring文档

The Spring IoC container creates exactly one instance of the object defined by that bean definition. This single instance is stored in a cache of such singleton beans, and all subsequent requests and references for that named bean return the cached object.

Spring IoC容器只创建该bean定义定义的对象的一个​​实例。此单个实例存储在此类单例bean的缓存中,并且该命名Bean的所有后续请求和引用都将返回缓存对象。

Because that bean has been cached inside the application context, it is never eligible for garbage collection until the application context is destroyed.

因为该bean已缓存在应用程序上下文中,所以在销毁应用程序上下文之前,它永远不会有资格进行垃圾回收。