Skip to main content
Please wait...
How Spring Dependency Injection Works
12 Apr, 2025

How Spring Dependency Injection Works

Spring Dependency Injection (DI) is a core feature of the Spring Framework that allows for the creation and management of objects and their dependencies in a flexible and decoupled manner. Here's a detailed explanation of how it works, the role of the @Autowired annotation, and a comparison between constructor-based and setter-based DI. 

 

How Spring Dependency Injection Works 

Inversion of Control (IoC): At the heart of DI is the principle of Inversion of Control (IoC). Instead of objects creating their dependencies, the control of creating and injecting dependencies is given to the Spring container. This leads to a more modular and testable codebase. 

 

Types of Dependency Injection

  • Constructor-Based Dependency Injection: Dependencies are provided through a class constructor. 

  • Setter-Based Dependency Injection: Dependencies are provided through setter methods. 

  • Field Injection: Dependencies are injected directly into fields using annotations (less common and generally discouraged due to issues with immutability and testability). 

 

Configuration: Dependencies can be configured in multiple ways: 

  • XML Configuration: Using XML files to define beans and their dependencies. 

  • Java-based Configuration: Using @Configuration and @Bean annotations to define beans in Java classes. 

  • Annotation-based Configuration: Using annotations like @Autowired, @Component, @Service, @Repository, and @Controller to automatically detect and inject dependencies. 

Bean Lifecycle: Spring manages the lifecycle of beans, from creation to destruction. When a bean is needed, the Spring container creates it and injects the required dependencies. The container also manages the scope of beans, such as singleton (one instance per Spring container) or prototype (a new instance each time it is requested). 

 

The @Autowired Annotation 

The @Autowired annotation is used to automatically inject dependencies into a class. When Spring encounters the @Autowired annotation, it looks for a suitable bean in the application context and injects it into the annotated field, constructor, or setter method. 

@Autowired Usage Examples: 

Field Injection: 

@Component 
public class Car { 
    @Autowired 
    private Engine engine; 
 
    public void start() { 
        engine.run(); 
    } 
} 

Constructor Injection: 

@Component 
public class Car { 
    private Engine engine; 
 
    @Autowired 
    public Car(Engine engine) { 
        this.engine = engine; 
    } 
 
    public void start() { 
        engine.run(); 
    } 
} 

Setter Injection: 

@Component 
public class Car { 
    private Engine engine; 
 
    @Autowired 
    public void setEngine(Engine engine) { 
        this.engine = engine; 
    } 
 
    public void start() { 
        engine.run(); 
    } 
} 

 

Conclusion 

Dependency injection in Spring is a powerful feature that promotes loose coupling, enhances testability, and provides flexible configuration options. The @Autowired annotation simplifies the process of injecting dependencies, making it easier to manage and maintain complex applications. Constructor-based DI is ideal for mandatory dependencies and promotes immutability, while setter-based DI offers flexibility and is suitable for optional dependencies.