0
0
SpringbootConceptBeginner · 3 min read

Constructor Injection in Spring: What It Is and How It Works

In Spring, constructor injection is a way to provide dependencies to a class by passing them through its constructor. This method ensures that the class receives all required dependencies when it is created, promoting immutability and easier testing.
⚙️

How It Works

Constructor injection works by giving a class all the things it needs right when it is created, through its constructor. Imagine you are baking a cake: constructor injection is like making sure you have all your ingredients measured and ready before you start mixing. This way, the cake (your class) can be made perfectly without missing anything.

In Spring, the framework automatically finds the right ingredients (dependencies) and hands them over to the class constructor. This means the class cannot exist without these dependencies, which helps avoid errors and makes the code easier to understand and maintain.

💻

Example

This example shows a simple service class that needs a repository. The repository is passed through the constructor, so Spring injects it automatically when creating the service.

java
import org.springframework.stereotype.Component;

@Component
class UserRepository {
    public String getUser() {
        return "User data";
    }
}

@Component
class UserService {
    private final UserRepository userRepository;

    public UserService(UserRepository userRepository) {
        this.userRepository = userRepository;
    }

    public String serve() {
        return "Serving: " + userRepository.getUser();
    }
}

// In a Spring Boot application, UserService will be created with UserRepository injected.
// Calling userService.serve() returns "Serving: User data"
Output
Serving: User data
🎯

When to Use

Use constructor injection when you want to make sure your class always has all its dependencies set and never changes them later. It is great for making your code easier to test because you can pass mock dependencies in tests.

Constructor injection is ideal for mandatory dependencies that the class cannot work without. For example, services that need repositories or configuration objects should use constructor injection to guarantee they are ready to use right after creation.

Key Points

  • Constructor injection passes dependencies through the class constructor.
  • It ensures dependencies are set once and cannot be changed later.
  • It promotes immutability and easier testing.
  • Spring automatically injects dependencies marked with @Autowired on constructors.
  • Best for required dependencies that the class needs to function.

Key Takeaways

Constructor injection provides dependencies through a class constructor, ensuring they are set at creation.
It helps create immutable and testable classes by requiring all dependencies upfront.
Spring automatically injects dependencies when constructors are annotated with @Autowired or have a single constructor.
Use constructor injection for mandatory dependencies your class cannot work without.
This approach leads to cleaner, safer, and easier-to-maintain code.