I am developing an application using Spring Boot. I currently have a class that is annotated as a @Component
with scope @Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
. The reason I define it as a prototype is because this class is a presenter and each client/view should have its own independent presenter instance. I cannot make it a singleton.
In this class, I want to be able to listen to events. I followed this tutorial and installed listeners using the @EventListener
annotation. However, when I debug the application I noticed that the event listener methods are evaluated in separate instances of the class. This is undesired, as I want them to have access to the state of the presenter.
I know that prototypes create a new instance every time they are injected, but I wouldn’t expect event listeners to also cause a separate instance to be created. Is this hypothesis correct, is this the reason new instances are created? In any case, how would I achieve my desired behavior?