Auto Wiring

In autowiring, the property name or the constructor argument is not declared within the configuration file.

The Spring container itself finds the type and name of the property.

It then matches the property type and name with other beans in the container based on their specified type or name.

This is done by setting the autowire property on each bean that you want Spring to autowire

The autowire property can take the following values:

byName :Finds a bean whose name matches the name of the property being wired.

byType :Finds a bean whose type matches the type of the property being wired.

constructor :Matches one or more beans in the container with the parameters of the constructors of the bean that is being wired.

autodetect :Wires the bean by first using constructor, and then byType, if there is a default no-args constructor.

Autowiring by name

If the name of a property matches the name of the bean that has to be wired into that property, the Spring framework can automatically wire that bean into the property.

For example:

<bean id="AB" class="SetterInject.FootballPlayer">

<property name="football" value="Adidas"/>

<property name="boots" ref="predator"/>

</bean>

Assume that you have declared predator as a <bean> having ID as boots, as shown in the following code snippet:

<bean id="boots" class="SetterInject.Predator"/>

The Spring framework can take advantage of this by automatically setting AB’s football boots through the autowire property, as shown in the following code snippet:

<bean id="AB" class="SetterInject.FootballPlayer" autowire="byName">

<property name="football" value="Adidas" />

</bean>

Therefore, byName autowiring provides a convention, according to which a property will be automatically wired to a bean of the same name.

Autowiring by type

You can also use the byType autowiring to wire a property to a bean.

Spring attempts to find a bean whose type is compatible with the property type.

For example:

<bean id="boots" class="SetterInject.Predator"/>

<bean id="AB" class="SetterInject.FootballPlayer" autowire="byType">

<property name="football" value="Adidas" />

</bean>

If Spring finds more than one bean, whose type is similar to the autowired property, it throws an exception. Therefore, there should only be one bean that can match the type of the autowired property.

Autowiring by constructor

You can wire your bean to a property by using constructor autowiring.

Here, you do not need to use the

Instead, you can simply set the autowire property to constructor.

As a result, Spring will automatically choose constructor arguments from the beans, defined in the configuration file

<bean id="Wilkinson" class="AutowireInject.RugbyPlayer" autowire="constructor">

<property name="shirtNumber" value="7"/>

</bean>

<bean id="NikeBoots" class="AutowireInject.RugbyBoots">

<property name="boots" value="Nike"/>

</bean>

The following line represents the output of the preceding code snippet

I am playing with shirt number 7 and Nike rugby boots.

Points To Remember

A limitation of this method is that Spring cannot choose which bean to autowire, if there are more than one bean that can be autowired.

Similarly, it cannot find which constructor to use, if there are more than one constructors.

In both of these situations, Spring throws an exception.

Autodetect autowiring

It is another type of autowiring that you can use when you cannot decide which type of wiring is to be used for wiring a bean and bean property.

When you set the autowire attribute to autodetect, the Spring container automatically decides the type of autowiring to use.

For example:

<bean id="AB" class="SetterInject.FootballPlayer" autowire="autodetect">

</bean>