Open In App

Spring Dependency Injection: @Autowired vs Constructor Injection

In Spring framework, dependency injection is a key idea that lets you inject dependencies into a class in preference to hard coding them. This provides loose coupling and makes the code extra maintainable and testable. There are distinct methods to perform dependency injection.

Common Approaches to Perform Dependency Injection

Below is the list of common approaches:



@Autowired vs Constructor Injection

@Autowired Annotation: @Autowired is a Spring annotation used for automatic dependency injection. It can be applied to fields, techniques, and constructors. When Spring encounters @Autowired, it routinely injects the proper bean at that factor. Consider the beneath instance for @Autowired Annotation:




import org.springframework.beans.factory.annotation.Autowired;
  
public class GeekService {
    @Autowired
    private GeekRepository geekRepository;
  
    // Other methods using geekRepository
}

Constructor Injection: Constructor injection is a shape of dependency injection in which the dependencies of a category are furnished through its constructor. In the Spring framework, constructor injection is a commonplace and endorsed manner to inject dependencies into a class.






import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
  
@Service
public class GeekService {
    private GeekRepository geekRepository;
  
    // Constructor Injection
    @Autowired
    public GeekService(GeekRepository geekRepository) {
        this.geekRepository = geekRepository;
    }
  
    // rest of the class
}

Key differences between @Autowired and Constructor Injection

Feature

@Autowired

Constructor Injection

Usage

Can be applied to fields, methods, or constructors.

Applied only to the constructor.

Annotation Location

Applied directly to fields or methods.

Applied to the constructor.

Dependencies Initialization

Dependencies can be injected after the object is created (method injection).

Dependencies are injected during object creation.

Preferred Approach

Suitable for scenarios where flexibility in changing dependencies at runtime is needed.

Preferred approach for better code readability, testability, and immutability.

Nullability

Fields may be null if not initialized properly, leading to potential Null Pointer Exceptions.

Nullability is less of a concern, as dependencies are initialized during construction.

Flexibility

Offers flexibility in changing dependencies dynamically at runtime.

Promotes a more rigid structure, discouraging changes to dependencies after construction.

Compile-time Safety

May result in runtime errors if dependencies are not present or misconfigured.

Ensures compile-time safety as dependencies are resolved during compilation.

Pros

Offers flexibility for dynamic setups.

Emphasizes simplicity and clarity.

Cons

May scatter dependencies, potentially reducing readability.

Centralizes dependencies, making code more straightforward.

Note: While @Autowired provides flexibility, Constructor Injection is favored for clean, maintainable code.

Conclusion

In summary, the choice between @Autowired and Constructor Injection in Spring, consider your project’s needs. @Autowired offers flexibility but may compromise code clarity and introduce runtime errors. Conversely, Constructor Injection, prioritizing simplicity and readability, is widely favored for clean coding practices, compile-time safety, and ease of testing. Below are the preferred recommendations.


Article Tags :