Autowiring in spring

Autowiring is an easy way of doing dependency injection in Spring.
Let’s see how we do dependency injection without autowiring

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;
	}
	

}

Car.java

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

public class Car {

}

Address.java

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

public class Address {

}

Now Person class has dependency on Car.
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
Lets see how can we accommodate this dependency

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

public class Address {

}

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;
	}

}
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>

Here we are adding the dependency by one more entry in config file So if Person gets one more dependency then we need to add more entry in config file like above.
So 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.
Lets see how it reduces the code even when we add dependency.

Step1 – Create Person class -> same as above
Step2 – Create Dependent classes – Car and Address – same as above
Step3 – Create beans.xml 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" 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.

Autowiring is of 4 types
1)By Name
2)By Type
3)Constructor
4)Autodetect – >autowire by constructor first if not then autowire by Type.

1)By Name -> the example which we have discussed above is autowire by name
Autowire by name means -> the name of the attribute in the Person class should be same as the bean id/name inside the spring configuration file.

Person class has an attribute car
So car should be available as a bean id/name in spring config file as below


And on the definition of Person bean we should have autowire=”byName”.

2)By Type -> The data type of the attribute should be same as the data type of the bean defined in the spring config file.

So in our case it is Person has car attribute and the datatype of ‘car’ attribute is “Car”.
So for this type of autowiring, spring looks for the Car type bean inside spring config 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>

Person class has a car attribute whose type is “Car” and we have the bean of type Car
We will have difficullty in this type of autowiring if we have many beans for the same type. 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");
	}

And auto wiring 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 use this way of autowiring then it will do automatic autowiring of 2 methods which we discussed above.
First autowire by constructor if matching constructor is found , if no matching constructor is found then autowire by type.

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 in config files with single tag

If we want to autowire all the beans in the config file by one of the above 4 types, then rather than specifying autowire strategy in each bean we can specify the same in beans tag which will apply it for all the beans.

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