Enjoying This Tutorial?
Subscribe to Ram N Java for high-quality, practical Java and Spring tutorials!
SUBSCRIBE NOWIntroduction
Auto-wiring in Spring allows the container to automatically resolve and inject collaborating beans without requiring manual <constructor-arg ref="..."/> declarations. When using constructor autowiring mode (autowire="constructor"), the Spring IoC container inspects the bean's constructor parameter types and injects matching bean instances configured in the context.
How Does Constructor Auto-Wiring Work?
- Type Matching: Similar to
byTypeautowiring, Spring searches the application context for beans whose data types match each argument of the constructor. - Constructor Selection: If a class contains multiple overloaded constructors, Spring selects the greediest constructor whose parameters can all be satisfied by existing beans in the context.
- Guaranteed Immutability: Because dependencies are passed through the constructor, the bean can be instantiated as fully initialized and immutable.
- No Multiple Matches Allowed: If more than one candidate bean of the exact same type exists in the container, Spring throws an
UnsatisfiedDependencyExceptiondue to ambiguity.
Step-by-Step Configuration Example
Consider an Employee class that receives an Address dependency strictly via its constructor:
1. The Java Model Class
package com.ram.model;
public class Employee {
private Address address;
// Constructor for autowiring
public Employee(Address address) {
this.address = address;
}
public void display() {
System.out.println("Employee address: " + address);
}
}
2. Configuring autowire="constructor" in XML
Notice that the employee bean does not need explicit <constructor-arg> tags:
<!-- Dependent Bean -->
<bean id="address" class="com.ram.model.Address">
<property name="city" value="Bangalore" />
<property name="state" value="Karnataka" />
</bean>
<!-- Employee Bean with Constructor Auto-Wiring -->
<bean id="employee"
class="com.ram.model.Employee"
autowire="constructor" />
3. Loading the Application Context
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
Employee emp = (Employee) context.getBean("employee");
emp.display();
Key Takeaway
Constructor autowiring combines the benefits of constructor-based dependency injection (immutability and mandatory dependency enforcement) with automated container wiring, keeping your XML configuration clean and concise.
Recommended Auto-Wiring Tutorials from Ram N Java
Click here to watch on Youtube:
https://www.youtube.com/watch?v=iyKVa2Za5As&list=UUhwKlOVR041tngjerWxVccw
Click the below Image to Enlarge:
![]() |
| What is Constructor Auto-wiring mode in Spring? |
pom.xml
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0%20http://maven.apache.org/maven-v4_0_0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.ram.core</groupId>
<artifactId>SpringDemo</artifactId>
<packaging>jar</packaging>
<version>1.0-SNAPSHOT</version>
<name>SpringDemo</name>
<url>http://maven.apache.org</url>
<properties>
<spring.version>5.0.2.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<!-- Spring 5 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>${spring.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring.version}</version>
</dependency>
</dependencies>
</project>
Address.java
public class Address
{
private String city;
private String state;
private String country;
public Address(String city, String state, String country)
{
this.city = city;
this.state = state;
this.country = country;
}
@Override
public String toString()
{
return "Address [city=" + city + ", state=" + state + ", country="
+ country + "]";
}
}
Employee.java
public class Employee
{
private String name;
private Address address;
public Employee(String name)
{
System.out.println("Employee(String name) constructor "
+ "is called by the Spring container");
this.name = name;
}
public Employee(String name, Address address)
{
System.out.println("Employee(String name, Address address) "
+ "constructor is called by the Spring container");
this.name = name;
this.address = address;
}
public void displayEmployeeDetails()
{
System.out.println(
"Employee [name=" + name + ", address=" + address + "]");
}
}
applicationContext.xml
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-4.3.xsd">
<bean id="address" class="com.ram.model.Address" >
<constructor-arg value="Chennai"></constructor-arg>
<constructor-arg value="Tamil Nadu"></constructor-arg>
<constructor-arg value="India"></constructor-arg>
</bean>
<bean id="employee" class="com.ram.model.Employee" autowire="constructor">
<constructor-arg value="Peter"></constructor-arg>
</bean>
</beans>
App.java
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import com.ram.model.Employee;
public class App
{
public static void main(String[] args)
{
ApplicationContext context = new ClassPathXmlApplicationContext(
"applicationContext.xml");
System.out.println("---------------------------------------");
Employee employee = (Employee) context.getBean("employee");
System.out.println(
"Got employee object from the ApplicationContext[Spring Container]");
employee.displayEmployeeDetails();
}
}
Output
INFO: Refreshing org.springframework.context.support.ClassPathXmlApplicationContext@5b37e0d2: startup date [Sun Jul 01 10:27:21 IST 2018]; root of context hierarchy
Jul 01, 2018 10:27:21 AM org.springframework.beans.factory.xml.XmlBeanDefinitionReader loadBeanDefinitions
INFO: Loading XML bean definitions from class path resource [applicationContext.xml]
Employee(String name, Address address) constructor is called by the Spring container
---------------------------------------
Got employee object from the ApplicationContext[Spring Container]
Employee [name=Peter, address=Address [city=Chennai, state=Tamil Nadu, country=India]
