Spring - Autowiring

Last Updated : 16 Jan, 2026

Spring Autowiring is a feature of the Spring Framework that automatically injects dependent objects into a bean, reducing the need for explicit configuration. It helps make applications loosely coupled, cleaner, and easier to maintain.

  • Automatically resolves and injects dependencies between Spring beans
  • Reduces boilerplate code and XML/annotation configuration

Types of Autowiring in Spring

Spring supports the following types of autowiring:

1. no (Default)

Spring does not perform autowiring. Dependencies must be defined explicitly.

XML
<bean id="state" class="sample.State">
    <property name="name" value="UP"/>
</bean>
<bean id="city" class="sample.City"/>

Key Points:

  • Default behavior
  • Full control over dependency injection
  • More boilerplate configuration

2. byName

Spring injects the dependency by matching the bean ID with the property name.

<bean id="city" class="sample.City" autowire="byName"/>

Key Points:

  • Uses setter-based injection
  • Property name must match the bean ID
  • Requires a public setter method

3. byType

Spring injects the dependency based on the class type

<bean id="city" class="sample.City" autowire="byType"/>

Key Points:

  • Setter-based injection
  • Throws an error if multiple beans of the same type exist
  • Bean name does not matter

4. constructor

Spring injects dependencies through the constructor.

<bean id="city" class="sample.City" autowire="constructor"/>

Key Points:

  • Constructor-based injection
  • Type-based resolution
  • Error occurs if no matching or multiple constructors are found

5. autodetect

Spring first tries constructor, then byType.

<bean id="city" class="sample.City" autowire="autodetect"/>

Note: This mode is deprecated in newer Spring versions and should be avoided.

Example: Autowiring Using ByName

This example demonstrates how Spring automatically injects a dependency by matching the bean ID with the property name using setter-based injection.

Step 1: Create the Project

Create a new Maven project in IntelliJ IDEA with the required GroupId, ArtifactId, and Java version.

  • Open IntelliJ -> New Project
  • Select Maven
  • JDK -> Java 8 or Java 11
  • GroupId: sample
  • ArtifactId: SpringAutowireDemo
  • Click Finish

Step 2: Project Structure

Organize the project into standard Maven directories for Java source files and resource files.

Structure

Step 3: Add Dependencies (pom.xml)

Add the spring-context dependency to enable core Spring features and dependency injection.

<dependencies>

<dependency>

<groupId>org.springframework</groupId>

<artifactId>spring-context</artifactId>

<version>5.3.30</version>

</dependency>

</dependencies>

Step 4: Create State Class

Create a simple POJO class representing the State with private fields and public getter/setter methods.

State.java

Java
package sample;

public class State {

    private String name;

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }
}

Step 5: Create City Class (Dependent Bean)

Create a City class that depends on the State class and exposes a setter method for byName autowiring.

City.java

Java
package sample;

public class City {

    private int id;
    private String name;

    // property name must match bean id
    private State state;

    public int getId() {
        return id;
    }

    public void setId(int id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    // required setter for byName autowiring
    public void setState(State state) {
        this.state = state;
    }

    public void showCityDetails() {
        System.out.println("City Id : " + id);
        System.out.println("City Name : " + name);
        System.out.println("State : " + state.getName());
    }
}

Step 6: Configure Spring Beans (XML)

Define Spring beans in applicationContext.xml and enable autowiring using the byName mode.

applicationContext.xml

XML
<?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.xsd">

    <!-- State Bean -->
    <bean id="state" class="sample.State">
        <property name="name" value="UP"/>
    </bean>

    <!-- City Bean (byName autowiring) -->
    <bean id="city" class="sample.City" autowire="byName"/>
</beans>

Step 7: Create Main Class

Load the Spring IoC container, retrieve the City bean, and invoke methods to verify dependency injection.

DemoApplication.java

Java
package sample;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;

public class DemoApplication {

    public static void main(String[] args) {

        ApplicationContext context =
                new ClassPathXmlApplicationContext("applicationContext.xml");

        City city = context.getBean("city", City.class);

        city.setId(1);
        city.setName("Varanasi");

        city.showCityDetails();
    }
}

Step 8: Run the Application

Run the main class to execute the program and observe the injected dependency output.

  • Right-click DemoApplication
  • Click Run

Output:

Out
output

Advantages of Autowiring

  • Reduces boilerplate code
  • Improves readability and maintainability
  • Simplifies dependency management

Disadvantages of Autowiring

  • Less control over dependency injection
  • Not suitable for primitive and String values
  • Can cause ambiguity when multiple beans exist
Comment