• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 806
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 1682
7
Android Mac OS X IntelliJ IDE Spring Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
MySQL Database Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you Mr.Bill. I will create a new Object for every request.
 
reply
    Bookmark Topic Watch Topic
  • New Topic