I am trying to get bean from spring session scoped bean. As far as I know that Beans instantiated based on session scope lives through the HTTP session. So how do I get this bean object from Http Session.
Asked
Active
Viewed 2,441 times
3
-
Just pull it out of the container like normal and Spring will ensure a shared instance for each session. . – Jasper Blues Jul 06 '14 at 08:10
2 Answers
3
You can access the ApplicationContext and retrieve beans from there.
ApplicationContext context = RequestContextUtils.getWebApplicationContext(request);
SessionInfo info = context.getBean("SessionInfo");-->Whatever bean you want

paul
- 12,873
- 23
- 91
- 153
2
You don't obtain the scoped bean from the Session object itself, you just get it from the Spring context in the same way that you get any other bean (e.g. using BeanFactory#getBean
). Spring will take care of synchronization between the context and the session.
While it's true that Spring will store the bean reference within the Session object, that's an internal implementation detail, you're not supposed to rummage around in there yourself. That's liable to change across different Spring versions, and the session attribute key under which the bean is stored is not documented.

skaffman
- 398,947
- 96
- 818
- 769
-
this is a bad answer, what if you have thread that needs to manipulate beans across sessions? – Enerccio Sep 24 '19 at 08:52