Dependency Injection in Spring Boot

December 18, 20242 min read

Dependency Injection in Spring Boot

Dependency Injection (DI) is a fundamental concept in Spring Boot that helps achieve loose coupling and increased testability. This guide demonstrates how to use DI in Spring Boot with practical examples in Java and Kotlin.


🌟 Why Use Dependency Injection?

Dependency Injection allows developers to manage and inject dependencies into classes without manually instantiating them. This leads to:

  • Better code modularity
  • Simplified testing
  • Easier maintenance

🌟 Prerequisites

📋 Ensure you have the following:

  • Java Development Kit (JDK) 17+
  • 📦 Maven or Gradle installed
  • 🔤 A Java IDE (e.g., IntelliJ IDEA, Eclipse)

🛠️ Step 1: Add Dependencies

Include the necessary Spring Boot dependencies:

  • Maven:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter</artifactId> </dependency>
  • Gradle:
implementation 'org.springframework.boot:spring-boot-starter'

📋 Step 2: Create a Service

Define a simple service to demonstrate DI.

Service

package com.example.demo.service; import org.springframework.stereotype.Service; @Service public class UserService { public String getUserById(String id) { return "User with ID: " + id; } public String getAllUsers() { return "Fetching all users."; } }

Service

package com.example.demo.service import org.springframework.stereotype.Service @Service class UserService { fun getUserById(id: String): String { return "User with ID: $id" } fun getAllUsers(): String { return "Fetching all users." } }

📖 Step 3: Inject Dependencies

Use annotations to inject the service into other components.

Controller

package com.example.demo.controller; import com.example.demo.service.UserService; import lombok.RequiredArgsConstructor; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController @RequestMapping("/api/users") @RequiredArgsConstructor public class UserController { private final UserService userService; @GetMapping("/{id}") public String getUserById(@PathVariable String id) { return userService.getUserById(id); } @GetMapping public String getAllUsers() { return userService.getAllUsers(); } }

Controller

package com.example.demo.controller import com.example.demo.service.UserService import org.springframework.web.bind.annotation.GetMapping import org.springframework.web.bind.annotation.PathVariable import org.springframework.web.bind.annotation.RequestMapping import org.springframework.web.bind.annotation.RestController @RestController @RequestMapping("/api/users") class UserController( private val userService: UserService ) { @GetMapping("/{id}") fun getUserById(@PathVariable id: String): String = userService.getUserById(id) @GetMapping fun getAllUsers(): String = userService.getAllUsers() }

▶️ Running the Application

Run the application using the following commands:

  • Spring Boot (Java/Kotlin):

    ./mvnw spring-boot:run

Access the API at http://localhost:8080/api/users.


🧪 Test the API

You can test the API using the following cURL commands:

  • Fetch all users:
curl -X GET http://localhost:8080/api/users
  • Fetch a user by ID:
curl -X GET http://localhost:8080/api/users/1

This guide demonstrates how to implement Dependency Injection in Spring Boot with Java and Kotlin, showcasing service injection and REST API integration.