0
0
Spring Bootframework~5 mins

Constructor injection (preferred) in Spring Boot

Choose your learning style9 modes available
Introduction

Constructor injection helps you give needed parts to a class when it is created. It makes your code clear and easy to test.

When you want to make sure a class always has what it needs to work.
When you want to write tests easily by giving fake parts to a class.
When you want to avoid mistakes by not allowing a class to be created without its parts.
When you want your code to be simple and clear about its needs.
When you want to use Spring Boot's recommended way to connect parts.
Syntax
Spring Boot
public class MyService {
    private final Dependency dependency;

    public MyService(Dependency dependency) {
        this.dependency = dependency;
    }

    // class methods
}

The dependency is passed in the constructor and saved in a final field.

Spring Boot automatically calls this constructor and gives the needed parts.

Examples
This example shows a service that needs a user repository to save users. The repository is given through the constructor.
Spring Boot
public class UserService {
    private final UserRepository userRepository;

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

    public void addUser(User user) {
        userRepository.save(user);
    }
}
Spring will create OrderService and give it a PaymentService automatically.
Spring Boot
@Service
public class OrderService {
    private final PaymentService paymentService;

    public OrderService(PaymentService paymentService) {
        this.paymentService = paymentService;
    }

    public void processOrder(Order order) {
        paymentService.pay(order.getAmount());
    }
}
Sample Program

This program shows how Spring Boot uses constructor injection. The WelcomeController needs a GreetingService. Spring gives SimpleGreetingService to it when creating the controller. The program prints a welcome message.

Spring Boot
import org.springframework.stereotype.Service;

interface GreetingService {
    String greet(String name);
}

@Service
class SimpleGreetingService implements GreetingService {
    public String greet(String name) {
        return "Hello, " + name + "!";
    }
}

@Service
class WelcomeController {
    private final GreetingService greetingService;

    public WelcomeController(GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    public String welcome(String user) {
        return greetingService.greet(user);
    }
}

public class Application {
    public static void main(String[] args) {
        // Simulate Spring Boot creating beans and injecting dependencies
        GreetingService greetingService = new SimpleGreetingService();
        WelcomeController controller = new WelcomeController(greetingService);

        System.out.println(controller.welcome("Alice"));
    }
}
OutputSuccess
Important Notes

Constructor injection makes your classes easier to test because you can give fake parts in tests.

Always use final fields with constructor injection to keep dependencies safe and unchangeable.

Spring Boot prefers constructor injection because it clearly shows what a class needs.

Summary

Constructor injection gives dependencies when creating a class.

It helps keep code clear, safe, and easy to test.

Spring Boot recommends constructor injection as the best way to connect parts.