Pages Navigation Menu

Coding is much easier than you think

Basics of Bean in Spring

Beans are the fundamental entities that we will be using while dealing with Spring. We make use of Beans as a backbone for the entire concept which enhances and supports loose coupling between components.. The various scopes that a bean can take over are :-


Scope Description
Singleton Only one instance of the bean will be available per Spring IOC container. By default, a bean has singleton scope.
Prototype A new bean instance is returned, every time there is a request
Request Returns a single bean instance per HTTP request
Session Returns a single bean instance per HTTP session
globalSession Returns a single bean instance per global HTTP session


Ref and ref local :

We have come across scenarios wherein, one of the attributes of a class is a class by itself. In such cases, we have seen that the value to that specific attribute is passed by using the tag


Note here that in case the definition for the beanIdABC is present in the same xml file in which it is referred to, then it is enough to place the reference instead as,

<ref local=”beanIdABC”€>

It is not mandatory to make such declaration. This is simply the best practice used in order to add on to the readability of the code.
In the next few tutorials, we will see in detail about the different scopes of a Bean. We will also be taking up practical examples of how they differ and the appropriate usage.