- Autowiring is a
mechanism
in Spring that allows the framework toautomatically detect
and inject the dependent beans to the properties of the target bean without the need forexplicit
configuration. - In simple words, when we apply autowiring, then Spring looks for the
dependent
beans in the container andautomatically injects
them into the target object. - Benefits:
- This can
save
a lot oftime and effort
, especially in large-scale applications where there are a large number ofdependencies
between objects.
- This can
- Drawbacks:
- No
control
of the programmer. - It can’t be used for
primitive
and not recommended forstring
values.
- No
`
<bean class="com.autowire.demo.beans.Account" id="account">
<property name="accountId" value="SV-101"/>
<property name="balance" value="50000.0"/>
</bean>
<bean class="com.autowire.demo.beans.Address" id="address">
<property name="houseNo" value="25"/>
<property name="city" value="Bhopal"/>
</bean>
<bean class="com.autowire.demo.beans.Customer" id="customer" autowire="constructor">
<constructor-arg index="0" value="Amit"/>
<constructor-arg index="1" value="27"/>
</bean>
`
- Setting
autowire
toconstructor
will inject dependencies using constructor injection. - Here the bean that has attribute
primary
set totrue
will take the preference and will be injected using constructor injection. - Constructor injection will inject all the values using constructor having four parameters.