Wednesday, January 2, 2019

Spring Request and Prototype Scope?

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 only ever be one bean instantiated, whereas that same bean scoped to Prototype in that same single HttpRequest would get 2 different instances.
HttpRequest scope
Employee employee1 = context.getBean("employee "); 
Employee employee2 = context.getBean("employee "); 
employee1 == employee2 ; //This will return true 
Prototype scope
Employee employee1 = context.getBean("employee "); 
Employee employee2 = context.getBean("employee "); 
employee1 == employee2 ; //This will return false

No comments:

Post a Comment

Spring Annotations