0

I have a Spring web app, it has a main controller which instantiates an object such as:

@Controller
public class testController {

private Boom boom = new Boom();

}

This object would for example have a Get method to display an arraylist of strings for the user to edit and save.

But if there are 2 sessions open, if I edit the list on one and save, then refresh the second session (second browser) it shows on that one. I don't want this to happen. I'd want the controller to make this object unique for each user viewing the application

What are my options for scoping this?

Travisty
  • 195
  • 2
  • 11
  • if you want to share the object between different user's session then consider making object scope as `gloabal-session` or `singleton` read more here https://www.tutorialspoint.com/spring/spring_bean_scopes.htm – Amit Jun 07 '17 at 08:39
  • Can't you make the Boom a request or session scope bean? Or just create it on fly rather than trying to reuse one instance – StanislavL Jun 07 '17 at 08:52
  • @StanislavL I've done some reading and used @Scope("session") to scope the controller, as the app is small so I'm okay with a new controller for each session. Otherwise scoping the Boom would work fine too! Thank you – Travisty Jun 07 '17 at 09:04
  • @Travisty , `Controller` must be singleton as a best practice and you should scope `Boom` bean as `Session`. – Amit Jun 07 '17 at 09:15

1 Answers1

0

As a best practice , Controller Object must be Singleton and beans which is being used by them, should have appropriate scope based on their functionality.

In you case as the bean Boom is used in a multiple requests across a user-session, hence you should create Boom with a scope of Session.

Read more about Spring scopes here and here.

Amit
  • 30,756
  • 6
  • 57
  • 88