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.
Written by
Şuayb Şimşek
Backend-focused fullstack developer sharing practical notes on Spring Boot, security, microservices, and cloud-native architecture.
Use annotations to inject the service into other components.
▶️ Running the Application
Run the application using the following commands:
Spring Boot (Java/Kotlin):
Run the application with either stack to confirm the baseline setup is working before deeper tests.
BASH
./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
Use this request to verify the list endpoint and validate the default response shape.
BASH
curl -X GET http://localhost:8080/api/users
Fetch a user by ID
Use this request to validate path-variable handling and single-resource retrieval behavior.
BASH
curl -X GET http://localhost:8080/api/users/1
🏁 Conclusion
You now have a practical Dependency Injection in Spring Boot implementation with a clear, production-friendly Spring Boot structure. As a next step, adapt configuration and tests to your own domain, then validate behavior under realistic traffic and failure scenarios.
Service
JAVAUserService.java
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
KOTLINUserService.kt
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."
}
}
Controller
JAVAUserController.java
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
KOTLINUserController.kt
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()
}