DEV Community

Cover image for Understanding Dependency Injection in Spring Boot
Tharindu Dulshan Fernando
Tharindu Dulshan Fernando

Posted on

8

Understanding Dependency Injection in Spring Boot

In simple terms DI means that objects do not initiate their dependencies directly. Instead they recive them from an external source.

  • When class A uses some functionality of class B, then its said that class A has a dependency of class B.

What is DI in Spring Framework?

Dependency Injection (DI) is a fundamental concept in Spring Framework that makes it possible to develop loosely coupled and easily testable code. In this blog, we'll explore what dependency injection is, how it works in Spring Boot, and provide examples to illustrate its usage.

There are three main types of dependency injection:

  1. Constructor Injection
  2. Setter Injection
  3. Field Injection

Constructor Injection

This is the most recommended way to acheive DI in Spring.It ensures that the dependency is not null and makes the class immutable.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestService {

    private final TestRepository repository;

    @Autowired
    public TestService(TestRepository  repository) {
        this.repository = repository;
    }

    public void performService() {
        repository.doSomething();
    }
}

Enter fullscreen mode Exit fullscreen mode

In the example above, TestService class declares a dependency on TestRepository. The dependency is injected via the constructor, which is annotated with @Autowired.

Setter Injection

Setter injection allows the dependency to be injected through a setter method. This method is less preferred than constructor injection because it allows the object to be in an incomplete state.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestService {

    private TestRepository repository;

    @Autowired
    public void setTestRepository(TestRepository repository) {
        this.repository = repository;
    }

    public void performService() {
        repository.doSomething();
    }
}

Enter fullscreen mode Exit fullscreen mode

In this example, TestService class has a setter method setTestRepository, which is used to inject the TestRepository dependency.

Field Injection

Field injection is the least preferred method because it makes the class less testable and harder to maintain. It involves annotating the dependency field directly with @Autowired.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

@Service
public class TestService {

    @Autowired
    private TestRepository repository;

    public void performService() {
        repository.doSomething();
    }
}

Enter fullscreen mode Exit fullscreen mode

Here, TestRepository is injected directly into the field of TestService using @Autowired.

Qualifiers

When there are multiple beans of the same type, you can use @Qualifier to specify which bean should be injected.

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

@Service
public class TestService {

    private final TestRepository myRepository;

    @Autowired
    public TestService(@Qualifier("specificRepository")TestRepository repository) {
        this.repository = repository;
    }

    public void performService() {
        repository.doSomething();
    }
}

Enter fullscreen mode Exit fullscreen mode

In this example, @Qualifier("specificRepository") specifies that a particular implementation of TestRepository should be injected.

Conclusion

Dependency Injection is a powerful pattern that helps to create loosely coupled, maintainable, and testable applications. Spring Boot leverages dependency injection to manage the lifecycle and configuration of application components, making development more straightforward and efficient. By understanding and applying constructor, setter, and field injection, as well as using qualifiers when necessary, you can create robust Spring Boot applications.

References

https://www.baeldung.com/spring-dependency-injection

https://www.baeldung.com/inversion-control-and-dependency-injection-in-spring

https://docs.spring.io/spring-framework/reference/core/beans/dependencies/factory-collaborators.html

Github : https://github.com/tharindu1998/SpringDependencyInjection

Image of AssemblyAI tool

Transforming Interviews into Publishable Stories with AssemblyAI

Insightview is a modern web application that streamlines the interview workflow for journalists. By leveraging AssemblyAI's LeMUR and Universal-2 technology, it transforms raw interview recordings into structured, actionable content, dramatically reducing the time from recording to publication.

Key Features:
🎥 Audio/video file upload with real-time preview
🗣️ Advanced transcription with speaker identification
⭐ Automatic highlight extraction of key moments
✍️ AI-powered article draft generation
📤 Export interview's subtitles in VTT format

Read full post

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Dive into an ocean of knowledge with this thought-provoking post, revered deeply within the supportive DEV Community. Developers of all levels are welcome to join and enhance our collective intelligence.

Saying a simple "thank you" can brighten someone's day. Share your gratitude in the comments below!

On DEV, sharing ideas eases our path and fortifies our community connections. Found this helpful? Sending a quick thanks to the author can be profoundly valued.

Okay