Description
Denis Zhdanov opened SPR-5248 and commented
The IoC container only tries to manage 'singleton' beans that are IS-A org.springframework.context.ApplicationListener. So, prototype beans that are IS-A ApplicationListener are not notified within the new events. There is no reason why the container shouldn't manage prototype beans during events propagation. It's able to register the prototype bean as a listener during its instantiation. The only thing to check is to reference that beans via weak references in order to avoid memory leaks.
From the other side, expected behavior is that application text propagates new event to all of the registered listeners (prototyped beans as well). User expect that because reference documentation mentions specially only life cycle event regarding prototype beans - '3.4.2. The prototype scope':
There is one quite important thing to be aware of when deploying a bean in the prototype scope, in that the lifecycle of the bean changes slightly. Spring does not manage the complete lifecycle of a prototype bean: the container instantiates, configures, decorates and otherwise assembles a prototype object, hands it to the client and then has no further knowledge of that prototype instance. This means that while initialization lifecycle callback methods will be called on all objects regardless of scope, in the case of prototypes, any configured destruction lifecycle callbacks will not be called. It is the responsibility of the client code to clean up prototype scoped objects and release any expensive resources that the prototype bean(s) are holding onto. (One possible way to get the Spring container to release resources used by prototype-scoped beans is through the use of a custom bean post-processor which would hold a reference to the beans that need to be cleaned up.)
There was a complain about that at the spring community already - see http://forum.springframework.org/showthread.php?t=62781.
Affects: 2.5.5
Issue Links:
- Support DisposableBean on prototype and @Configurable beans using weak references [SPR-9391] #14027 Support DisposableBean on prototype and
@Configurable
beans using weak references - Prototype beans that implement ApplicationListener shouldn't be created during container initialization [SPR-5247] #9921 Prototype beans that implement ApplicationListener shouldn't be created during container initialization