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
2
3
4
5
6
7
8
9
10
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;
    }
    
 
}
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
2
3
4
5
package com.kb.autowiring1;
 
public class Car {
 
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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>
<?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
2
3
4
5
package com.kb.autowiring1;
 
public class Address {
 
}
package com.kb.autowiring1;

public class Address {

}


Create Person.java

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
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;
    }
 
}
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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?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>
<?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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?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>
<?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
2
<bean id="car" class="com.kb.autowiring1.Car"/>
<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
2
3
4
<bean id = "person" class ="com.kb.autowiring1.Person" autowire="byType">
</bean>
<bean id="car" class="com.kb.autowiring1.Car"/>
</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
2
3
4
5
6
public class Person {
    Car car;
    public Person(Car car) {
        this.car=car;
        System.out.println("person");
    }
public class Person {
	Car car;
	public Person(Car car) {
		this.car=car;
		System.out.println("person");
	}

autowiring is done as below

1
2
3
4
<bean id = "person" class ="com.kb.autowiring1.Person" autowire="constructor">
</bean>
<bean id="car" class="com.kb.autowiring1.Car"/>
</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
2
3
4
<bean id = "person" class ="com.kb.autowiring1.Person" autowire="autodetect">
</bean>
<bean id="car" class="com.kb.autowiring1.Car"/>
</beans>
<bean id = "person" class ="com.kb.autowiring1.Person" autowire="autodetect">
</bean>
<bean id="car" class="com.kb.autowiring1.Car"/>
</beans>

Or

1
2
3
4
<bean id = "person" class ="com.kb.autowiring1.Person" autowire="default">
</bean>
<bean id="car" class="com.kb.autowiring1.Car"/>
</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
2
3
4
5
6
7
8
9
10
<?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>
<?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

Karibasappa G C (KB)
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