0

I'm working with Spring 3 MVC (I'm quite new to it...) and I need some database info to be loaded after user's loggin and stay available until user logs out or closes the web page (I could query the DB each time I need it but it wouldn't be efficient). As far as I know, this should be done through session's management. I've found the following tutorial for managing session data:

http://richardchesterwood.blogspot.com.es/2011/03/using-sessions-in-spring-mvc-including.html

Do you think this is a good starting point? Is the 3rd option mentioned the best approach for what I need? When am I supposed to populate the objects that I need to keep in the user's session? Is it OK to do it when the user logs in?

I was also wondering if it is possible to keep some information that should be common to all users (something like a session that is valid for each user).

Thank you very much!!

5
  • 1
    You can also use the @SessionAttributes annotation on your controller to indicate that certain Model Attributes should be put into Session. I find it easier to do, as all I need to worry about is putting something in Model, and Spring handles the rest. To clean up things, I use the SessionStatus on my handler method, and do a .setComplete() on it, which invalidates the session. Here is a blog posting with an example: fruzenshtein.com/spring-mvc-session Commented Jun 10, 2013 at 14:54
  • That's an interesting way of managing some session data, but what if you need to manage heavy-weight classes? What I need to know is the most (or almost) efficient way of managing heavy-weight classes as session data. Thanks!! Commented Jun 11, 2013 at 8:06
  • I don't think that link is a good starting point. Commented Jun 11, 2013 at 10:01
  • When I have a class that needs to be in session, I use the above coupled with the @ModelAttribute. When placed on a handler method's property, this annotation tells Spring to de-serialize the incoming HTTP parameters into the appropriate object instance. However, there is another trick: Place the (at)ModelAttribute on a method that returns an instance of your object and takes no parameters. In this case, what Spring does is calls this method to create an instance of your object when one does not exist in the Model (and, by extension, Session). Commented Jun 11, 2013 at 10:57
  • Here is a blog posting speaking to how to use the two mentioned annotations together: vmustafayev4en.blogspot.com/2012/10/… Commented Jun 11, 2013 at 10:59

1 Answer 1

2

I don't think that link is a good starting point. it overly complicates things. There is often no need to change scope of beans, as recommended in the article. In fact having a controller instantiated per request is very inefficient. A new instance of an object would created for every single request - I literally can't think of a worse idea for a scalable performant website.

Spring security makes it easy. Just have a user object containing the relevant data that is returned by your implenetation of the user service.

There is no need to change scope of anything, keep it simple and use the default of singleton (will make scaling easier). And if really necessary use a session attribute.

Sign up to request clarification or add additional context in comments.

4 Comments

Yeah, that's what I thought at the beginning. I don't want to create a new instance every time it is requested: what I want is precisely avoid the construction of that object each time, and keep already loaded data. Thanks, I'll take a look to spring security as soon as I can. In the meantime I'll be using the HttpSession, though I know is no elegant.
By the wsay, I was even thinking further: is it possible to keep some data (objects) available to all users? I mean, I have some static data common to all users that is loaded once from database and don't need to be queried each time a new user logs in. Is there any way to do this?
Please, does anybody have any idea about my last question?
its easy just define a bean then inject where you need it.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.