• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Request scope vs Prototype scope

 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi ,

Whats the difference b/w request scope vs prototype scope in terms of a webapplication..where there are 2 beans
1 is request scope
1 is prototype bean...
And both end up being used for the processing...

For x request...x request beans & x prototype beans are created ..so what is the difference between the two..

Regards
 
Mark Spritzler
ranger
Sheriff
Posts: 17278
6
IntelliJ IDE Mac Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A Kumar wrote:Hi ,

Whats the difference b/w request scope vs prototype scope in terms of a webapplication..where there are 2 beans
1 is request scope
1 is prototype bean...
And both end up being used for the processing...

For x request...x request beans & x prototype beans are created ..so what is the difference between the two..

Regards


Prototype creates a brand new instance everytime you call getBean on the ApplicationContext. Whereas for Request, only one instance is created for an HttpRequest. So in a single HttpRequest, I can call getBean twice on Application and there will ever be one bean instantiated, whereas that same bean scoped to Prototype in that same single HttpRequest would get 2 different instances.

HttpRequest scope
Mark mark1 = context.getBean("mark");
Mark mark2 = context.getBean("mark");
mark1 == mark2; //This will return true

Prototype scope

Mark mark1 = context.getBean("mark");
Mark mark2 = context.getBean("mark");
mark1 == mark2; //This will return false


Hope that clears it up for you.

Mark
 
A Kumar
Ranch Hand
Posts: 980
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Mark!!!
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic