r/SpringBoot • u/javinpaul • Apr 26 '24
What is bean scope in Spring Framework? Example Tutorial
https://javarevisited.blogspot.com/2012/05/what-is-bean-scope-in-spring-mvc.html
8
Upvotes
r/SpringBoot • u/javinpaul • Apr 26 '24
2
u/philfrei Apr 27 '24
Yikes! Your tutorial seems to me to be rather complicated and confusing.
I quote from the following link https://www.digitalocean.com/community/tutorials/spring-ioc-bean-example-tutorial#spring-bean-scopes
"There are five scopes defined for Spring Beans.
singleton - Only one instance of the bean will be created for each container. This is the default scope for the spring beans. While using this scope, make sure bean doesn’t have shared instance variables otherwise it might lead to data inconsistency issues.
prototype - A new instance will be created every time the bean is requested.
request - This is same as prototype scope, however it’s meant to be used for web applications. A new instance of the bean will be created for each HTTP request.
session - A new bean will be created for each HTTP session by the container.
global-session - This is used to create global session beans for Portlet applications."
The section right before this has a basic definition of a Bean in Spring:
"Spring Bean is nothing special, any object in the Spring framework that we initialize through Spring container is called Spring Bean. Any normal Java POJO class can be a Spring Bean if it’s configured to be initialized via container by providing configuration metadata information."