I am new to Spring and I am currently using it in one of my projects. I learned that the Spring container holds all the beans and the scope of all the beans is \"singleton
From Spring documentation (3.5.2 The prototype scope):
In contrast to the other scopes, Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, and otherwise assembles a prototype object, and hands it to the client, with no further record of that prototype instance.
Simply put - once you create and obtain a reference to prototype
scoped bean, it is the only reference existing in the JVM. Once this references gets out of scope, the object will be garbage collected:
void bar() {
Object foo = ctx.getBean("foo")
}
The moment you leave bar()
method there aren't any other references to new instance of foo
, which means it is eligible for garbage collection. The consequence of this model is:
Thus, although initialization lifecycle callback methods are called on all objects regardless of scope, in the case of prototypes, configured destruction lifecycle callbacks are not called.