Autowiring & Scope of Bean
We have seen the Spring bean and its metadata in the earlier article. In this article, we will see more about two of them Auto wiring and scope.
Injecting dependent beans into the target beans is called Dependency Injection/ Auto wiring.
Spring can auto resolve the dependencies using the configuration provided to the spring container.
Spring will reduce the need for constructor arguments when we use auto wiring in the configuration using XML based.
Auto Wiring can be done in 2 ways:
1. Explicit Auto wiring / Manual Injection
a. <property name = “ “ ref = “ “ />
b. <constructor-arg name = “ “ ref = “ “ />
We have already seen this type of injection setter and constructor injection in the Dependency Injection part.
2. Auto wiring / Auto Injection
In this type auto wiring can be done in 3 ways:
1. byName
2. byType
3. constructor
Limitations:
1. This is only possible in Object type/ reference type beans
2. There is a possibility of ambiguity.
3. Readability of the spring bean configuration file will need improvement.
1.byName:
This type of auto wiring follows setter injection.
The container finds a dependent spring bean class/ object based on the id that is matching with the target class.
There is no possibility of an ambiguity problem because the bean ids in the IOC container are unique.
<bean id="courier" class="src.courier.Amazon"/>
<!-- courier is the type of the Object what we used in the Flipkart object-->
<bean id="dtdc" class="src.courier.Dtdc"/>
<bean id="bluedart" class="src.courier.BlueDart"/>
<bean id="flip" class="src.services.Flipkart" autowire="byName"/>
In the above XML file, we have 3 beans that are implementing courier type interface. so if it is by Name container will look for the courier type of object with the same name(courier) while injecting that in the Flipkart service.
2. byType
This type of auto wiring also follows setter injection.
There will be a possibility of ambiguity problem because in the container there can be different beans of the same type.
We can resolve the ambiguity problem using “primary = true”.
<bean id="amazon" class="src.courier.Amazon"/>
<bean id="dtdc" class="src.courier.Dtdc" primary="true"/>
<bean id="bluedart" class="src.courier.BlueDart"/>
<bean id="flip" class="src.services.Flipkart" autowire="byType"/>
In the above configuration file if we have auto wire using byType which looks for the type of courier object we have 3 classes that are implementing the courier interface and we have 3 beans where we get an ambiguity problem. we get rid of this using “primary = true” for one of the beans.
3. constructor
This type of auto wiring follows constructor injection.
There will be no possibility of ambiguity as we have only one bean with a name.
In this type, the constructor param name should match the dependent class bean id for the auto wiring to happen.
<bean id="amazon" class="src.courier.Amazon"/>
<bean id="dtdc" class="src.courier.Dtdc"/>
<bean id="courier" class="src.courier.BlueDart"/>
<!-- courier is the constructor name what we used in the Flipakrt object creation-->
<bean id="flip" class="src.services.Flipkart" autowire="constructor"/>
In the above configuration file, we have 3 beans of courier type and auto wire is using the constructor. we make one bean of courier type so that when we are using the constructor injection it picks the courier bean.
Scope attribute in Spring bean:
We can define the bean scope when the container creates the bean. This is powerful and flexible because we can choose the scope of objects we create instead of having the scope of the object at the java class level.
Spring framework supports 5 types of scopes
1. singleton(default)
2. prototype
3. request
4. session
5. global session
out of which 3 are available only if we use web-aware Application Context.
We will be seeing the other two types here:
1. Singleton:
This is the default scope for any bean in the spring.
IOC container will never make the spring class a singleton java class, but it creates only one object stores this object in the internal cache and returns the object whenever the bean is used.
<!-- Configuring dependent bean-->
<bean id="student" class="src.model.Student" scope="singleton"/>
<!-- configuration target bean-->
<bean id="university" class="src.University">
<constructor-arg ref="student"/>
2. Prototype:
IOC container will create a new object for the spring bean class for every getBean().
The container does not keep the scope of spring bean in the cache.
<!-- Configuring dependent bean-->
<bean id="student" class="src.model.Student" scope="prototype"/>
<!-- configuration target bean-->
<bean id="university" class="src.University">
<constructor-arg ref="student"/>
Please do follow for more articles and learn with me.
Special Thanks to Nitin M who made these all concepts very easy.