Skip to content

Prototype application context event listeners are not notified within the events [SPR-5248] #9922

Closed
@spring-projects-issues

Description

@spring-projects-issues

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:

Metadata

Metadata

Assignees

Labels

in: coreIssues in core modules (aop, beans, core, context, expression)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions