Lesson

Spring Autowiring

spring/Spring Boot

Spring Autowiring

1. Introduction

Spring Autowiring is a feature that automatically injects dependencies into a bean.

It helps to:

  • Reduce manual configuration

  • Make code clean and maintainable

  • Support loose coupling


2. What is Autowiring

Autowiring means:

  • Spring automatically finds and injects dependent objects

  • No need to manually configure dependencies


3. Types of Autowiring

Spring supports the following types:


1. no (Default)

  • No autowiring

  • Dependencies must be set manually

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

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

2. byName


  • Dependency injected using bean name


  • Property name must match bean id


  • Uses setter method

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

3. byType


  • Dependency injected based on type (class)


  • Bean name does not matter


  • Error if multiple same type beans

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

4. constructor


  • Injection through constructor


  • Uses type matching

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

5. autodetect


  • First tries constructor, then byType


  • Deprecated in modern Spring

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

4. Example of Autowiring (byName)


Step 1: Create State Class

plaintext
package sample;

public class State {

    private String name;

    public String getName() {
        return name;
    }

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

Step 2: Create City Class

plaintext
package sample;

public class City {

    private int id;
    private String name;

    private State state;

    public void setState(State state) {
        this.state = state;
    }

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

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

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

Step 3: XML Configuration

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

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

Step 4: Main Class

plaintext
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();
    }
}

5. Advantages


  • Less configuration


  • Cleaner code


  • Easy dependency management


6. Disadvantages


  • Less control


  • Issues when multiple beans exist


  • Not suitable for primitive and String injection


7. Conclusion


  • Autowiring simplifies dependency injection


  • byName and byType are commonly used


  • Annotation-based autowiring is preferred in Spring Boot