×
☰ See All Chapters

Autowiring in Spring

The application objects must be aware of one another and communicate with one another to get their job done. In an online shopping application, for instance, an order manager component may need to work with a product manager component and a credit card authorization component. All of these will likely need to work with a data access component to read from and write to a database.

The act of creating such associations between application objects is commonly referred to as wiring which is the essence of dependency injection.

The container will create the objects, wire them together, configure them, and manage their complete lifecycle from cradle to grave (or new to finalize).

Autowiring means injecting the dependencies automatically to the beans without configuring them in configuration file.

  • In autowiring, spring container dynamically detects and assigns dependent values to bean properties without configuring them explicitly.  

  • In autowiring there is no need of using <property> and <constructor-arg>.  

  • Autowiring is possible only for properties which are of user defined data type.  

  • Spring provides five flavors of autowiring , programmer can give instructions to perform autowiring in different modes by using the autowire attribute of <bean> tag. autowire attribute supports five different values for five different modes of autowiring, they are  

    1. byname  

    2. byType  

    3. constructor  

    4. autodetect  

    5. no  

byName

Attempts to find a bean in the container whose name (or ID) is the same as the name of the property being wired. If a matching bean is not found, the property will remain unwired.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="https://www.springframework.org/schema/beans"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

 

        <bean id="adrress" class="com.java4coding.EmplyeeAdress">

                <property name="city" value="Bangalore"></property>

        </bean>

 

        <bean id="studentbean" class="com.java4coding.EmployeeBean" autowire="byName">

                <property name="name" value="Manu Manjunatha"></property>

        </bean>

</beans>

package com.java4coding;

 

public class EmployeeBean {

        private String name;

        private EmplyeeAdress adrress;

        public String getName() {

                return name;

        }

        public void setName(String name) {

                this.name = name;

        }

        public EmplyeeAdress getAdrress() {

                return adrress;

        }

        public void setAdrress(EmplyeeAdress adrress) {

                this.adrress = adrress;

        }

}

byType

Attempts to find a single bean in the container whose type matches the type of the property being wired. If no matching bean is found, the property will not be wired. If more than one bean matches, an org.springframework.beans.factory.UnsatisfiedDependencyException will be thrown.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="https://www.springframework.org/schema/beans"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

 

        <bean id="adrress" class="com.java4coding.EmplyeeAdress">

                <property name="city" value="Bangalore"></property>

        </bean>

 

        <bean id="studentbean" class="com.java4coding.EmployeeBean" autowire="byType">

                <property name="name" value="Manu Manjunatha"></property>

        </bean>

 

</beans>

constructor

Works only for constructor injection. Tries to match up one or more beans in the container with the parameters of one of the constructors of the bean being wired. In the event of ambiguous beans or ambiguous constructors, an org.springframework.beans.factory.UnsatisfiedDependencyException will be thrown.

<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="https://www.springframework.org/schema/beans"

        xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"

        xsi:schemaLocation="https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans.xsd">

 

        <bean id="adrress" class="com.java4coding.EmplyeeAdress">

                <property name="city" value="Bangalore"></property>

        </bean>

 

        <bean id="studentbean" class="com.java4coding.EmployeeBean" autowire="constructor">

        </bean>

</beans>

package com.java4coding;

 

public class EmployeeBean {

        private EmplyeeAdress adrress;

        public EmployeeBean(EmplyeeAdress adrress) {

                this.adrress = adrress;

        }

}

autodetect

Attempts to autowire by constructor first and then using byType. Ambiguity is handled the same way as with constructor and byType wiring.

no

Indicates that this bean doesn’t support autowiring.

Limitations of autowiring

  • We cannot use autowiring for properties which are of primitive and String types.  

  • There is a possibility of getting an ambiguous state, when spring container finds multiple dependent objects for a single property.  


All Chapters
Author