Spring Dependency Injection Methods

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

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

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

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/

--

--

--

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

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

JAVA: Array, Strings, Vectors

Opex Analytics Puzzle: Eye Color Islands

Variables and Data Types In Python

The Neighborhood Walk Story

Cutting the fat: Lightweight Perl OO modules

My PHPMD Configuration on PHPStorm

Building a Slack app with Firebase as a backend

Automating Privacy by Design, Part 2

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Nil Seri

Nil Seri

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

More from Medium

RestTemplate with Spring Boot —

Get rid of CORS origin or resource sharing error [Spring Boot-Angular]:

Spring Boot Reactive Web Application — WebFlux

Liquibase — Exploring with Spring boot and Maven