I have an application, that has 2 ApplicationContexts A and B, where A is the parent context of B. B overwrites some beans from A, but also uses some of its beans. For the frontend, I use Wicket and I have two Wicket applications AppA and AppB that use the respective Spring ApplicationContext.
The problem now arises with a Session-scoped bean sessionBean, for which I have 2 different implementations: A defines a factory method returning an instance of sessionBeanA, B has a factory method with same signature returning an instance of sessionBeanB.
The user now opens AppB and in the backend, we get an instance of sessionBeanB, wherever we inject a sessionBean, as we would expect.
However, if the user now leaves AppB and opens AppA (still with the same underlying session), we get an instance of sessionBeanB injected in A, because the SessionScope object still holds the previously created bean object.
How can I prevent that a session-scoped bean leaks in a different ApplicationContext? ApplicationContext A should not have to bother with beans from B...
Any help is highly appreciated.
It's possible to extend the
SessionScopefrom Spring to also consider the ApplicationContext by using aCustomScopeConfigureras follows:where
CustomSessionScopeis:and you can use it in the Java Spring configuration class with: