aspose file tools*
The moose likes Spring and the fly likes Should I use singleton or create new object for each request? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Spring
Bookmark "Should I use singleton or create new object for each request?" Watch "Should I use singleton or create new object for each request?" New topic
Author

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

chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

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


Love all, trust a few, do wrong to none.
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18906
    
    8

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

Joined: Mar 07, 2010
Posts: 1680
    
    7

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).


[How To Ask Questions][Read before you PM me]
chaitanya karthikk
Ranch Hand

Joined: Sep 15, 2009
Posts: 806

Thank you Mr.Bill. I will create a new Object for every request.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Should I use singleton or create new object for each request?