title: Storing session-scope beans in @controller
body: <p>I am trying to store my session-scoped user beans in a singleton-scoped controller throughout their lifecycle in session. So whenever a user is being connected, I want to store it in an array with the rest of the users those who keep their sessions. </p>
  
  <p>I know about injecting a session-scoped bean into a @Controller through proxy beans so that i have defined my session-scoped user beans as follow,</p>
  
  <pre><code>@Bean
  @Scope(value="session", proxyMode = ScopedProxyMode.TARGET_CLASS)
  public IUser user ()
  {
      IUser user = new MyUser();
      return user;
  }
  </code></pre>
  
  <p>I have used @Autowire annotation to inject that bean into my controller class as below,</p>
  
  <pre><code>@Autowired
  private IUser sessionUser;
  </code></pre>
  
  <p>So whenever a user is getting connected, I am storing that user in a ConcurrentHashMap which is defined and added as below,</p>
  
  <pre><code>    private ConcurrentHashMap&lt;Integer,IUser&gt;    userMap     = new ConcurrentHashMap&lt;&gt;(50,0.9f,2);
  
  
      public void addUser(IUser user)
      {
          if(user == null) return;
  
          IUser retUser =  userMap.putIfAbsent(user.getDbid(),user);
          //...
      }
  </code></pre>
  
  <p>So everything is working when the first user gets connected, I store its reference to map. Let's assume first user reference is  </p>
  
  <pre><code>us.com.soemthing.orm.model.MyUser@135debf
  </code></pre>
  
  <p>Then let us assume the second user gets connected whose reference is,</p>
  
  <pre><code>us.com.soemthing.orm.model.MyUser@28zbdh
  </code></pre>
  
  <p>From the references, I can see that my session-scoped beans work fine as their reference is different. <strong>However</strong>, problems start when execution goes into <strong>addUser</strong> method. <strong>Even before</strong> adding the second user to the map, I check my userMap and see that <strong>user object it is storing replaced with the second one which is MyUser@28zbdh.</strong> So at the end, after adding the second user, My user map looks like this,</p>
  
  <pre><code>Map --&gt; "1"- us.com.soemthing.orm.model.MyUser@28zbdh
  Map --&gt; "2"- us.com.soemthing.orm.model.MyUser@28zbdh
  </code></pre>
  
  <p>So that references are being updated always with the last one. I know that they are the proxy object to real objects but how I can store them?</p>
  
  <p>Thanks</p>
  
  <p><strong>[EDIT] I wanted to provide additional information.</strong></p>
  
  <p>I am calling addUser from another singleton bean as, userInDBMemory.addUser(sessionUser);    userInDBMemory is another singleton bean where i add my session user to a ConcurrentHashMap actually. I want to store my current online users on a map as I would like to search and query them without going to the database. So i would like to keep online users (who has a session in context) in memory for easier and faster access. To handle session expires, every online user sends a heartbeat to server to show he is online, I have a scheduled thread on server running in every X minutes and if it finds any user who didn't get heartbeat from the user for a while then it removes it from the map as it means user went offline. To summary my case I have a main controller where i get requests then the chain is like this: @Controller->singleton application bean->Singleton inMemoryDB bean (where I define my map and add user) My SessionUser session-scoped bean is @Autowired in @Controller and i pass it to other singleton beans as a parameter.Thanks for the response.     </p>