2
votes

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.

2
Just pull it out of the container like normal and Spring will ensure a shared instance for each session. . - Jasper Blues

2 Answers

2
votes

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.

2
votes

You can access the ApplicationContext and retrieve beans from there.

    ApplicationContext context = RequestContextUtils.getWebApplicationContext(request);
    SessionInfo info = context.getBean("SessionInfo");-->Whatever bean you want