Spring Dependency Injection Methods

Nil Seri
3 min readApr 26, 2022

Spring Dependency Injection Methods, Their Advantages & Disadvantages

Photo by Diana Polekhina on Unsplash
  • “Dependency Injection” helps in implementing “Inversion of Control (IoC)”.
  • The responsibility of object creation and injecting the dependencies is given to the framework (i.e. Spring).
  • We can implement “Dependency Injection” with:
    - 1️⃣ Field Injection
    - 2️⃣ Setter Injection
    - 3️⃣ Constructor Injection

Field Injection

Spring assigns the required dependencies directly to the fields on annotating with @Autowired annotation. Field Injection uses reflection to set the values of private variables.

Field Injection Example — https://dzone.com/articles/spring-di-patterns-the-good-the-bad-and-the-ugly
  • 👎🏻 Hard to test. You either need to bring up the Spring Context, or use some Spring utilities to perform dependency injection for testing.
  • 👎🏻 Dependencies are unnecessarily mutable.
  • 👎🏻 Prone to circular dependency issues (throwing BeanCurrentlyInCreationException).

Constructor Injection

The dependencies required for the class are provided as arguments to the constructor. Annotation of constructors (@Inject or @Autowired) for dependency injection has been optional since Spring Framework version 4.2.

Constructor Injection Example — https://dzone.com/articles/spring-di-patterns-the-good-the-bad-and-the-ugly
  • 👍🏻 final fields can be initialized in the constructor, our dependencies can be immutable (because a constructor’s signature is the only possible way to create objects now). This also helps with robustness and thread-safety.
  • 👍🏻 Prevents NullPointerExceptions (The IoC container makes sure that all the arguments provided in the constructor are available before passing them into the constructor).
  • 👍🏻 Easy to test (easier than with setter injection)
  • 👎🏻 Any subclass of a class using constructor injection must have a constructor that calls the parent constructor. You can avoid avoid injected dependencies in parent components — usually done through composition over inheritance (by using Interface).
  • 👍🏻 Recommended by Spring.
  • 👎🏻 Prone to circular dependency issues.

Setter Injection

We provide the required dependencies as field parameters to the class and the values are set using the setter methods of the properties. We have to annotate the setter method with the @Autowired annotation.

Setter Injection Example — https://dzone.com/articles/spring-di-patterns-the-good-the-bad-and-the-ugly
  • 👍🏻 Not hard to test.
  • 👍🏻 Immune to circular dependency issues.
  • 👎🏻 Dependencies are unnecessarily mutable.

Constructor Injection Using Project Lombok 🌶

  • Declare a final property of the interface type
  • Annotate the class using Project Lombok’s @RequiredArgsConstructor
  • When you need to add another bean, simply declare a final property (or vice versa).
Constructor Injection with Project Lombok — https://springframework.guru/best-practices-for-dependency-injection-with-spring/

Happy Coding!

--

--

Nil Seri

I would love to change the world, but they won’t give me the source code | coding 👩🏻‍💻 | coffee ☕️ | jazz 🎷 | anime 🐲 | books 📚 | drawing 🎨