Autowiring in spring

Autowiring is an easy way of doing dependency injection in Spring.

Let’s see how we do dependency injection without autowiring


Create Person.java

  1. package com.kb.autowiring1;
  2.  
  3. public class Person {
  4.    
  5.     Car car;
  6.     public Car getCar() {
  7.         return car;
  8.     }
  9.     public void setCar(Car car) {
  10.         this.car = car;
  11.     }
  12.    
  13.  
  14. }
package com.kb.autowiring1;

public class Person {
	
	Car car;
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	

}


Create Car.java

  1. package com.kb.autowiring1;
  2.  
  3. public class Car {
  4.  
  5. }
package com.kb.autowiring1;

public class Car {

}

Now Person class has dependency on Car class.

So we can achieve this in spring using dependency injection as below

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xmlns:context="http://www.springframework.org/schema/context"
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8.    http://www.springframework.org/schema/context
  9.    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  10.  
  11. <bean id = "person" class ="com.kb.autowiring1.Person">
  12. <property name="car" ref="car"/>    
  13. </bean>
  14. <bean id="car" class="com.kb.autowiring1.Car"/>
  15. </beans>
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id = "person" class ="com.kb.autowiring1.Person">
<property name="car" ref="car"/>    
</bean>
<bean id="car" class="com.kb.autowiring1.Car"/>
</beans>

It’s a normal way of injecting car to person.

Now if we come up with one more dependency on Person ie Address

  1. package com.kb.autowiring1;
  2.  
  3. public class Address {
  4.  
  5. }
package com.kb.autowiring1;

public class Address {

}


Create Person.java

  1. package com.kb.autowiring1;
  2.  
  3. public class Person {
  4.    
  5.     Car car;
  6.     Address add;
  7.     public Car getCar() {
  8.         return car;
  9.     }
  10.     public void setCar(Car car) {
  11.         this.car = car;
  12.     }
  13.     public Address getAdd() {
  14.         return add;
  15.     }
  16.     public void setAdd(Address add) {
  17.         this.add = add;
  18.     }
  19.  
  20. }
package com.kb.autowiring1;

public class Person {
	
	Car car;
	Address add;
	public Car getCar() {
		return car;
	}
	public void setCar(Car car) {
		this.car = car;
	}
	public Address getAdd() {
		return add;
	}
	public void setAdd(Address add) {
		this.add = add;
	}

}


Create Spring Configuration File(.xml)

So accordingly we are adding the dependency by one more entry in configuration file at line number 13

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xmlns:context="http://www.springframework.org/schema/context"
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8.    http://www.springframework.org/schema/context
  9.    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  10.  
  11. <bean id = "person" class ="com.kb.autowiring1.Person">
  12. <property name="car" ref="car"/>  
  13. <property name="add" ref="add"/>    
  14. </bean>
  15. <bean id="car" class="com.kb.autowiring1.Car"/>
  16. <bean id="add" class="com.kb.autowiring1.Address"/>
  17. </beans>
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id = "person" class ="com.kb.autowiring1.Person">
<property name="car" ref="car"/>   
<property name="add" ref="add"/>    
</bean>
<bean id="car" class="com.kb.autowiring1.Car"/>
<bean id="add" class="com.kb.autowiring1.Address"/>
</beans>


If Person gets one more dependency then we need to add one more entry in configuration file like above.
To reduce this kind of adding extra code for each dependency , we can go for Spring’s feature “Autowiring”. Thanks a ton for Autowiring feature.

Autowiring is of 4 types

1) byName

2) byType

3) constructor

4) autodetect


Let us see how it reduces the code

1) byName

The name of the attribute in the Person class should be same as the bean id/name inside the spring configuration file.

Person class and dependent classes are same as above, We need to change only in spring configuration file

  1. <?xml version="1.0" encoding="UTF-8"?>
  2.  
  3. <beans xmlns="http://www.springframework.org/schema/beans"
  4.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5.     xmlns:context="http://www.springframework.org/schema/context"
  6.     xsi:schemaLocation="http://www.springframework.org/schema/beans
  7.    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8.    http://www.springframework.org/schema/context
  9.    http://www.springframework.org/schema/context/spring-context-3.0.xsd">
  10.  
  11. <bean id = "person" class ="com.kb.autowiring1.Person" autowire="byName">
  12. </bean>
  13. <bean id="car" class="com.kb.autowiring1.Car"/>
  14. <bean id="add" class="com.kb.autowiring1.Address"/>
  15. </beans>
<?xml version="1.0" encoding="UTF-8"?>

<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="http://www.springframework.org/schema/beans
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

<bean id = "person" class ="com.kb.autowiring1.Person" autowire="byName">
</bean>
<bean id="car" class="com.kb.autowiring1.Car"/>
<bean id="add" class="com.kb.autowiring1.Address"/>
</beans>

Now we have not written the code for giving reference to dependent objects, still it happened just by a small piece of code in bean tag
—-> autowire=”byName”.
Yes this way of injecting dependencies automatically is called Autowiring.

Here Person class has an attribute “car” and “add”
On the definition of Person bean we should have autowire=”byName”.
“car” and “add” attributes should be available as a bean id/name in spring configuration file as below

  1. <bean id="car" class="com.kb.autowiring1.Car"/>
  2. <bean id="add" class="com.kb.autowiring1.Address"/>
<bean id="car" class="com.kb.autowiring1.Car"/>
<bean id="add" class="com.kb.autowiring1.Address"/>

2)byType

The data type of the attribute should be same as the data type of the bean defined in the spring configuration file.

So in our case,Person has car attribute and its datatype is “Car”.
If you use byType of autowiring then spring looks for the “Car” type bean inside spring configuration file.

So beans config file should have below entry

  1. <bean id = "person" class ="com.kb.autowiring1.Person" autowire="byType">
  2. </bean>
  3. <bean id="car" class="com.kb.autowiring1.Car"/>
  4. </beans>
<bean id = "person" class ="com.kb.autowiring1.Person" autowire="byType">
</bean>
<bean id="car" class="com.kb.autowiring1.Car"/>
</beans>


Note:If we have many beans for the same datatype then we will get an error.Don’t worry we will touch this problem later.

3)constructor

In this method , spring searches for the type of constructor argument inside spring config file

Person class has below constructor

  1. public class Person {
  2.     Car car;
  3.     public Person(Car car) {
  4.         this.car=car;
  5.         System.out.println("person");
  6.     }
public class Person {
	Car car;
	public Person(Car car) {
		this.car=car;
		System.out.println("person");
	}

autowiring is done as below

  1. <bean id = "person" class ="com.kb.autowiring1.Person" autowire="constructor">
  2. </bean>
  3. <bean id="car" class="com.kb.autowiring1.Car"/>
  4. </beans>
<bean id = "person" class ="com.kb.autowiring1.Person" autowire="constructor">
</bean>
<bean id="car" class="com.kb.autowiring1.Car"/>
</beans>

4)autodetect or default

If we choose Autowiring type is autodetect/default then Autowire by constructor first , if not then autowire by Type.

If we use this way of autowiring then it will do automatic autowiring of 2 methods

1) autowire by constructor, if matching constructor is found

2) autowire by type, if no matching constructor is found then.

  1. <bean id = "person" class ="com.kb.autowiring1.Person" autowire="autodetect">
  2. </bean>
  3. <bean id="car" class="com.kb.autowiring1.Car"/>
  4. </beans>
<bean id = "person" class ="com.kb.autowiring1.Person" autowire="autodetect">
</bean>
<bean id="car" class="com.kb.autowiring1.Car"/>
</beans>

Or

  1. <bean id = "person" class ="com.kb.autowiring1.Person" autowire="default">
  2. </bean>
  3. <bean id="car" class="com.kb.autowiring1.Car"/>
  4. </beans>
<bean id = "person" class ="com.kb.autowiring1.Person" autowire="default">
</bean>
<bean id="car" class="com.kb.autowiring1.Car"/>
</beans>

Note: Best way is to refer dependent objects explicitly without autowiring to have better readability and to avoid confusion.

Autowiring all beans by ‘single tag’ in configuration file

If we want to autowire all the beans in the config file by one of the above 4 types, then we can achieve this by using default-autowire=” “ element in beans tag which will apply it for all the beans rather than specifying autowire strategy for each bean.

Look at the below example how we do it

  1. <?xml version="1.0"encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="http://www.springframework.org/schema/beans
  5. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
  6. default-autowire="byType">
  7. <bean id="somebean1" class="some class 1"/>
  8. <bean id="somebean2" class="some class 2"/>
  9. <bean id="somebean3" class="some class 3"/>
  10. </beans>
<?xml version="1.0"encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd"
default-autowire="byType">
<bean id="somebean1" class="some class 1"/>
<bean id="somebean2" class="some class 2"/>
<bean id="somebean3" class="some class 3"/>
</beans>

Now all the 3 beans defined inside beans tag are having autowire feature enabled and it is byType as specified in beans tag.

Note:

By default default-autowire is set to none in beans tag, so none of the beans are enabled with autowiring by default

About the Author

Founder of javainsimpleway.com
I love Java and open source technologies and very much passionate about software development.
I like to share my knowledge with others especially on technology 🙂
I have given all the examples as simple as possible to understand for the beginners.
All the code posted on my blog is developed,compiled and tested in my development environment.
If you find any mistakes or bugs, Please drop an email to kb.knowledge.sharing@gmail.com

Connect with me on Facebook for more updates

Share this article on