37

What's the best way of storing session related data of a user (like, for example a log of recent actions a user has done) in a Spring MVC (2.5) web application ?

Using the classic javax.servlet.http.HttpSession or by specifying scope="session" in controller beans, and storing the data in a session object ?

Petar Minchev
  • 46,889
  • 11
  • 103
  • 119
dakull
  • 719
  • 2
  • 7
  • 14

1 Answers1

37

Session-scoped beans (using scope="session") is the cleanest approach. This removes the need to interact with the session yourself.

If you want to autowire a session-scoped bean in to the controller, you either need to make the controller session-scoped itself, or use a scoped-proxy to wire it into a singleton controller, as described here. Either approach is valid.

skaffman
  • 398,947
  • 96
  • 818
  • 769
  • thank you, a cleaner approach was exactly what i was searching for – dakull Sep 02 '09 at 13:32
  • 1
    Thankyou for this excellent answer, I had been using the HttpSession directly up until now. Making the Controller Request scoped (ie the old Throwaway Controller) also works and avoids the need for the scoped-proxy. Are there any downsides to this? – Dick Chesterwood May 06 '10 at 19:07
  • 4
    @Dick: request-scoped beans bring a performance penalty, but as long as your controller doesn't have an expensive custom init process, or your traffic isn't too high, it shouldn't be significant. – skaffman May 06 '10 at 19:14
  • 2
    can you please provide an example of work with session-scoped beans in the controller? – vacuum Mar 26 '12 at 08:26