views:

59

answers:

2

Hi,

I have the following problem, I have configured the following class which should be stored in session.

<bean id="Users" class="com.doolloop.DlUser" scope="session">
<aop:scoped-proxy/>
</bean>

Then I in my Dispatcher servlet I would like to access this class user and set

@RequestMapping(value="/authenticate.do",method = RequestMethod.POST)
          public String sampleAuthentication(@Valid Person person, BindingResult result,
                  Map model,HttpServletRequest request){
                    ...... /some code
  HttpSession session = request.getSession();
          DlUser user = (DlUser) session.getAttribute("Users");
                  /// some uses for user object

}

The problem is that I'm always getting null Value of user object.

What am I doing wrong?

Second problem, I read in articles that accessing HttpSession is not thread-safe, how can it be done safe way? Should be kind of Singleton? Why this is not Thread Safe operation?

Thank you in advance.

Danny.

+1  A: 

objects scoped "session" are not STORED in session, they are available to the session through the regular direct injection paradigm. It's just that they are session-tied. So, if you need to have this available, you need to inject this bean into your controller. Read here for more info.

Anatoly G
Does it mean that this bean will exist the same period as the Http session exists?
danny.lesnik
yes, that's correct, but do read the answer from Bozho. If you are injecting this bean into a longer-living bean, the effect will not be the same as you expect. You are better off using session.getAttribute() to get the object, or use AOP to do proxying for the session object - look at the example here for more detail: http://wheelersoftware.com/articles/spring-session-scoped-beans.html. Essentially, this link explains how to create a proxy for the session-scoped bean, so the proxy is the one that gets the correct instance of object matching what's in session
Anatoly G
+1  A: 

That's a special case when you want to inject a bean with a shorter scope into a bean with a longer scope (i.e. a session-scoped bean into a singleton-scoped bean)

You can use a lookup-method:

<bean id="yourSingletonBean"
    class="your.singleton.BeanClass">
    <lookup-method name="getDLUser" bean="Users"/>
</bean>

You'll need to make your singleton bean abstract, and create an abstract method public DLUser getDLUser()

That, however, is not a perfect solution. The idea is to hold in the session as little as possible. If you don't need to store a bean in the session, but only the current user, you can easily call session.setAttribute(..) when the user logs in, and then access the current user with session.getAttribute(..), without the user being a bean at all.

A way that I chose for the current user in my application is to create a custom @SessionAttribute annotation. See this question on how to achieve that.

Bozho