Is there any way to destroy all created session scoped bean of a particular class (and let spring recreate when needed) ?
For example, 2 users visit the application, and then there will be 1 bean for each of the user. I would like to destroy all these 2 beans.
Use case:
a admin is updating the menu bar. menu bar list data are stored in a session scoped bean. the admin's bean should be destroyed and the menu bar should be updated. and of course the others' menu should be updated as well, so the others bean should be destroyed as well.
note 1: different user may see different menu, so the bean is session scoped, not singleton.
note 2: i do not want to invalidate whole session of the user, but just only that bean
i found this way to destroy the current scope bean. but i am not able to destroy bean from other session.
((ScopedObject) myBean).removeFromScope();
Thanks a lot !!
Destroying beans and recreating them seems overkill and a little messy too.
For such approaches I would favor events and Spring provides a Spring Event API (
spring-contextdependency).You could inject an
ApplicationEventPublisherinstance in the bean class that provokes the state change for some other beans and these beans could register a even listener (@EventListener(condition = "..."))to read this change event.For example the publisher :
Subscribers/Listeners :
You could see more information here.