Wednesday, January 2, 2019

what is the difference between request and session scope in spring?


If the bean scope is request and, a user makes more than one request for a web page in his user session, then on every request a new bean would be created.
Whereas if the scope is defined as session for the bean, if a user makes a request for a web page more than once, then on every request same bean would be returned as long as the requests are within the same user session and made from a client which is capable of maintaining the session.

HttpRequest scope for same user session in single HttpRequest :


Employee employee1 = context.getBean("employee "); 
Employee employee2 = context.getBean("employee "); 
employee1 == employee2 ; //This will return true  
 

HttpRequest scope for same user session in multiple HttpRequest : 

Employee employee1 = context.getBean("employee "); 
Employee employee2 = context.getBean("employee "); 
employee1 == employee2 ; //This will return false
 

Session scope for same user session in multiple HttpRequest : 

Employee employee1 = context.getBean("employee "); 
Employee employee2 = context.getBean("employee "); 
employee1 == employee2 ; //This will return true

No comments:

Post a Comment

Spring Annotations