Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Should I use singleton or create new object for each request?

 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all, I am having an ajax controller. This controller will receive post data. I am validating the post data and then if it has some errors I am sending the errors as a json object.

The error messages, codes are in the BindingResult. So I created a new class called JsonValidatedResponse. In this class I am extracting the error codes and fields and adding them to appropriate lists. Then sending the marshaled object to client.

My doubt is should if I want to autowire this JsonValidatedResponse object, which scope should I use? Request scope or singleton scope? However I am not creating any session to have session scope.

As I see request scope is what I need I think so. Can anyone please suggest me? This is how my JsonValidatedResponse looks likeThis is what I am doing in the controller
 
Paul Clapham
Sheriff
Posts: 21416
33
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You shouldn't use a singleton unless there's an extremely good reason to do so.

You don't appear to have an extremely good reason. Or any reason for that matter. At least you haven't posted any... Does that help?
 
Bill Gorder
Bartender
Posts: 1682
7
Android IntelliJ IDE Linux Mac OS X Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is in essence a DTO (data transfer object). Its sole purpose is to pass information from your controller to your view. Typically I would not make these type of objects Spring beans. In any object though it is good practice to make your fields private unless there is a good reason not to (currently they are package private).
 
chaitanya karthikk
Ranch Hand
Posts: 806
Java MySQL Database Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you Mr.Bill. I will create a new Object for every request.
 
Don't get me started about those stupid light bulbs.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic