Stop Using Setter Injection Constructor Injection is Better |Video upload date:  · Duration: PT14M35S  · Language: EN

Why constructor injection is better than setter injection in Spring Boot Learn benefits like immutability clearer dependencies and easier testing

If your Spring Boot app uses setter injection everywhere you might enjoy surprises at runtime and tests that hate you. Constructor injection forces required dependencies to show up at creation time which makes your Java code easier to reason about and your unit tests far less dramatic.

What constructor injection actually gives you

Think of constructor injection as the blunt honesty of dependency injection. Required services become constructor parameters and you can mark fields final for immutability. That means fewer null pointer surprises and no need to hunt down mysterious mutations at 2am.

Minimal example that does the job

public class OrderService {
  private final PaymentService paymentService
  public OrderService(PaymentService paymentService) {
    this.paymentService = paymentService
  }
}

This is the version you want to read in code reviews. The dependency sits right where you expect it. IDE navigation is happier and unit testing is straightforward because you can new up OrderService with a mock or fake PaymentService.

Why setter injection tempts you and why it fails you

Setter injection looks cute at first. Short methods, quick wiring, and a false sense of flexibility. The catch is hidden dependencies that may be null unless someone remembered to call the setter. That leads to mutable fields which encourage accidental state changes and surprising runtime behavior.

Setter example that makes your life harder

public class OrderService {
  private PaymentService paymentService
  public void setPaymentService(PaymentService paymentService) {
    this.paymentService = paymentService
  }
}

Lombok and Spring Framework life hacks

If constructor boilerplate makes you groan try Lombok. Adding RequiredArgsConstructor to a Spring component saves typing while keeping the safety of constructor wiring.

@RequiredArgsConstructor
@Component
public class OrderService {
  private final PaymentService paymentService
}

This pattern plays nicely with Spring Boot and preserves immutability and clear dependency contracts.

What to do when constructors create cycles

Spring may refuse some cycles when you wire everything via constructors. That is not a bug. It is a design smell telling you to rethink the coupling. In those rare cases refactor to break the cycle, introduce a factory or provider, or extract a new service that reduces the mutual dependency. Do not reach for setter injection as a band aid because that just hides the real problem.

Practical DI best practices for Java and Spring Boot

  • Prefer constructor injection for mandatory dependencies and mark fields final for immutability
  • Use setter injection only for truly optional collaboration that may be absent at runtime
  • Leverage Lombok RequiredArgsConstructor to reduce ceremony while keeping safety
  • When you see a constructor cycle treat it as a signal to refactor not as an excuse for mutable wiring
  • Write unit tests by newing up the class with mocks or fakes to keep tests fast and obvious

Bottom line if you care about safer Spring Framework code, cleaner DI, and happier unit testing, favor constructor injection. Setter injection has its place but it should not be the default. Your future self will send thanks and maybe a sarcastic trophy.

I know how you can get Azure Certified, Google Cloud Certified and AWS Certified. It's a cool certification exam simulator site called certificationexams.pro. Check it out, and tell them Cameron sent ya!

This is a dedicated watch page for a single video.