Skip to main content

Bean Scopes

 Bean Scopes


-> Bean scope will decide how many objects should be created for a spring bean.

-> The default scope of spring bean is singleton (that means only one object will be created).

-> We can configure below scopes for spring bean.

1) singleton

2) prototype

3) request

4) session

Note: For singleton beans objects will be created when IOC starts.

-> For prototype beans when we call context.getBean(..) method then object will be created .

-> For prototype beans every time new object will be created.

-> To save memory spring framework made the default scope as singleton.

<bean id="motor" class="in.ashokit.Motor" scope="prototype" />

<bean id="car" class="in.ashokit.Car" />

Autowiring

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

->In application several classes will be available.

-> One class wants to talk to another class.

-> We are using IOC container to perform that dependency injection.

-> We are giving instruction to IOC to inject dependent object into target object using 'ref' attribute.

<bean id="billCollector" class="in.ashokit.BillCollector">

<constructor-arg name="payment" ref="upi" />

</bean>

-> Using "ref" attribute we are telling to IOC which object it has to inject.

=> This process is called as Manual Wiring.

=> Spring IOC supports Autowiring concept  that means Spring IOC having capability to identify the dependent and inject dependent into target

=> Autowiring having mode.

1) byName 

2) byType

3) constructor

4) no

-> byName means if target class variable name matched with any bean id/name in bean configuration file then IOC will consider that as dependent bean and it will inject that dependent bean object into target object.

<bean id="dieselEng" class="in.ashokit.beans.DieselEngine" />

<bean id="car" class="in.ashokit.beans.Car" autowire="byName"/>


=> byType means it will check data type of the variable.  With Datatype of variable if any bean class is configured then it will identify that as dependent and it will inject into target.

         <bean id="xyz" class="in.ait.beans.DieselEngine" />

<bean id="car" class="in.ait.beans.Car" autowire="byType">

Note: We can configure one class for multiple times with different ids then we will get ambiguity problem in byType scenario.

-> In byType mechanism if we have more than one bean matching with type then we will get ambiguity problem.

=> To overcome ambiguity problem we need to use 'autowire-candidate=false'

<bean id="xyz" class="in.ait.beans.DieselEngine" autowire-candidate="false"/>

<bean id="abc" class="in.ait.beans.DieselEngine" />

<bean id="car" class="in.ait.beans.Car" autowire="byType">

</bean>

Note: When we configure autowiring with "byName" or "byType" it is performing setter injection by default and setter method is mandatory in target bean.

=> If we want to perfom Autowiring through constructor then we can use 'constructor' mode.

<bean id="xyz" class="in.ait.beans.DieselEngine" autowire-candidate="false"/>

<bean id="abc" class="in.ait.beans.DieselEngine" />

<bean id="car" class="in.ait.beans.Car" autowire="constructor"/>

=> In 'constructor' byType will be used to identify dependent bean object.


Comments

© 2020 The JavaDS Hub

Designed by Open Themes & Nahuatl.mx.