JavaIntermediate#spring

What are the different types of dependency injection in Spring?

Constructor injection (recommended, immutable, testable), setter injection (optional dependencies), and field injection (@Autowired directly on a field, discouraged for testability reasons).

Example
@Service
class OrderService {
  private final PaymentService ps;
  @Autowired
  OrderService(PaymentService ps) { this.ps = ps; } // constructor injection
}

Related Questions

1
JavaBeginner#spring

What is the difference between @Component, @Service, @Repository, and @Controller?

Open
2
JavaIntermediate#spring

What is Spring Boot auto-configuration?

Open
3
JavaIntermediate#spring

What is the purpose of @RestController vs @Controller in Spring MVC?

Open