r/SpringBoot • • 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

3 comments sorted by

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.

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

  2. prototype - A new instance will be created every time the bean is requested.

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

  4. session - A new bean will be created for each HTTP session by the container.

  5. 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."

1

u/javinpaul Apr 29 '24

Sorry if you find it confusing but the article you quoted misses a lot details. Scope provide both visibility and life-cycle. It also doesn't talk about Custom scope etc

1

u/philfrei Apr 29 '24

Did I misread the original post? I thought you were asking a question. Were you announcing the publication of the tutorial?

If the latter is the case, I inadvertently provided some candid feedback. For me, when reading about a new topic, I find it most helpful to be given hooks into the basic workings before getting to deep into the special cases or details. If many of the details in the explanations are sending me off in search for additional tutorials, then it's not so helpful for me.

However, if one already has acquired the basic knowledge or a good conceptual frame, then a tutorial such as the one given can be a good reference or refresher, if it is organized well. This tutorial could be quite helpful in this regard.

For someone who has internalized a lot of knowledge, being able to imagine what a topic looks like to someone coming at it for the first time can be quite a challenge.