DEV Community

Cover image for Dependency Injection in Spring: Simplifying Your Code
Igor Venturelli
Igor Venturelli

Posted on • Edited on • Originally published at igventurelli.io

Dependency Injection in Spring: Simplifying Your Code

In software development, clean, maintainable, and testable code is crucial, especially in complex applications. One of the key principles that helps achieve these qualities is Dependency Injection (DI), a core feature of the Spring Framework. DI promotes loose coupling between components, making it easier to manage dependencies and test individual components in isolation. In this post, we’ll break down what DI is, how Spring leverages it, and why it matters for building scalable applications. We'll also walk through some practical code examples to solidify your understanding.

What is Dependency Injection?

In traditional programming, when a class needs to use another class’s functionalities, it creates an instance of that dependency within its code. However, this tightly couples the two classes, which can make the code rigid, difficult to test, and challenging to change or extend. Dependency Injection is a design pattern that addresses these issues by injecting dependencies from an external source instead of creating them within a class.

With DI, objects are supplied with their dependencies, usually at runtime, which promotes loose coupling—meaning the class only needs to know the interface of its dependency, not the specific implementation. This allows for more flexible and interchangeable code components, making the code easier to test and adapt.

How Does Spring Implement Dependency Injection?

Spring makes DI simple and powerful by managing the object lifecycle and their dependencies. In Spring, dependencies are typically injected using three methods:

  1. Constructor Injection: Dependencies are provided through a class’s constructor.
  2. Setter Injection: Dependencies are assigned via public setter methods.
  3. Field Injection: Dependencies are injected directly into the fields of a class.

In Spring Boot applications, DI is configured via annotations, which makes setup and maintenance straightforward.

Example of Dependency Injection in Spring Boot

Let’s walk through an example to demonstrate how DI works in Spring Boot.

Step 1: Define a Service Interface

First, let’s define an interface GreetingService that provides a method for generating greetings:

public interface GreetingService {
    String getGreeting();
}
Enter fullscreen mode Exit fullscreen mode

Step 2: Implement the Service

Now, let’s create two implementations of this interface to see how Spring can inject them dynamically.

import org.springframework.stereotype.Service;

@Service("morningGreetingService")
public class MorningGreetingService implements GreetingService {
    @Override
    public String getGreeting() {
        return "Good morning!";
    }
}

@Service("eveningGreetingService")
public class EveningGreetingService implements GreetingService {
    @Override
    public String getGreeting() {
        return "Good evening!";
    }
}
Enter fullscreen mode Exit fullscreen mode

Here, we have two classes implementing the same interface, but each provides a different greeting message.

Step 3: Inject the Dependency into a Controller

Next, let’s create a controller that uses GreetingService. We’ll use Constructor Injection here, which is often preferred in Spring for its clarity and testability.

import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class GreetingController {

    private final GreetingService greetingService;

    public GreetingController(@Qualifier("morningGreetingService") GreetingService greetingService) {
        this.greetingService = greetingService;
    }

    @GetMapping("/greet")
    public String greet() {
        return greetingService.getGreeting();
    }
}
Enter fullscreen mode Exit fullscreen mode

In this example, we use @Qualifier to specify which implementation to inject (morningGreetingService). Spring handles the creation and injection of the GreetingService instance, promoting loose coupling. To switch to a different greeting service, we simply change the qualifier without modifying the controller logic.

Step 4: Testing with Dependency Injection

One of the biggest advantages of DI is the ability to test components independently. For instance, we could use a mock GreetingService implementation to test GreetingController without relying on the actual service.

import static org.mockito.Mockito.*;
import org.junit.jupiter.api.Test;
import org.springframework.boot.test.context.SpringBootTest;
import static org.junit.jupiter.api.Assertions.assertEquals;

@SpringBootTest
public class GreetingControllerTest {

    @Test
    public void testGreet() {
        GreetingService mockService = mock(GreetingService.class);
        when(mockService.getGreeting()).thenReturn("Hello, Test!");

        GreetingController controller = new GreetingController(mockService);
        assertEquals("Hello, Test!", controller.greet());
    }
}
Enter fullscreen mode Exit fullscreen mode

By using a mock service, we ensure that GreetingController can be tested in isolation, a benefit made possible by dependency injection.

Benefits of Dependency Injection in Spring

  1. Loose Coupling: Classes rely on interfaces, not specific implementations, making code more flexible and extensible.
  2. Increased Testability: Components can be tested independently with mock or stub dependencies.
  3. Better Code Organization: Dependencies are clearly defined, making the structure and flow of your application easier to understand.
  4. Easy to Extend: Adding new implementations or changing dependencies is seamless with DI, as it’s managed externally by the framework.

Conclusion

Dependency Injection is a game-changer for structuring clean, testable, and modular applications. By leveraging DI in Spring, we can manage complex relationships between components while promoting best practices like loose coupling and separation of concerns. Whether you’re developing a simple web application or a large-scale enterprise system, DI helps keep your code organized, flexible, and maintainable. So, as you continue your Spring journey, remember that DI is more than just a pattern—it’s a cornerstone of well-structured software design.


Let’s connect!

➡️ LinkedIn
➡️ Original Post
☕ Buy me a Coffee ☕

Top comments (0)