Controller se Service layer ko call karne ke 4 main tareeke hote hain.
Industry me Constructor Injection = BEST PRACTICE maana jaata hai ✅
✅ 1️⃣ Constructor Injection (⭐ RECOMMENDED)
Service:
@Service
public class UserService {
public UserDTO getUser(Long id) {
return new UserDTO(1L, "Bhupendra", "bhu@gmail.com");
}
}
Controller:
@RestController
@RequestMapping("/users")
public class UserController {
private final UserService userService;
// Constructor injection
public UserController(UserService userService) {
this.userService = userService;
}
@GetMapping("/{id}")
public ResponseEntity<UserDTO> getUser(@PathVariable Long id) {
UserDTO dto = userService.getUser(id); // 👈 service call
return ResponseEntity.ok(dto);
}
}
✔ Most safe
✔ Easy to test
✔ Immutable (final)
✔ Industry standard
⚠️ 2️⃣ Field Injection (@Autowired) – NOT recommended
@RestController
@RequestMapping("/users")
public class UserController {
@Autowired
private UserService userService; // 👈 directly injected
@GetMapping("/{id}")
public ResponseEntity<UserDTO> getUser(@PathVariable Long id) {
return ResponseEntity.ok(userService.getUser(id));
}
}
❌ Hard to test
❌ Field hidden dependency
❌ Not good practice in big projects
⚠️ 3️⃣ Setter Injection (rare use)
@RestController
@RequestMapping("/users")
public class UserController {
private UserService userService;
@Autowired
public void setUserService(UserService userService) {
this.userService = userService;
}
}
✔ Optional dependency me use hota hai
❌ Mostly avoid kiya jata hai
✅ 4️⃣ Lombok ke saath (cleanest way 😎)
@RestController
@RequestMapping("/users")
@RequiredArgsConstructor
public class UserController {
private final UserService userService; // Lombok constructor bana dega
@GetMapping("/{id}")
public ResponseEntity<UserDTO> getUser(@PathVariable Long id) {
return ResponseEntity.ok(userService.getUser(id));
}
}
✔ Clean code
✔ Constructor injection hi hai
✔ Industry favorite
🧠 Flow yaad rakho:
Controller ---> Service ---> Repository ---> DB
↑ ↑
HTTP Request Business Logic
Controller:
sirf request handle kare
Service:
business logic likhe
🎯 Interview One-Liner:
Service layer ko Controller me call karne ka best tareeka hai Constructor Injection using
@Autowiredor Lombok@RequiredArgsConstructor.
❓ Extra: Multiple services inject karna
public class OrderController {
private final OrderService orderService;
private final UserService userService;
public OrderController(OrderService orderService,
UserService userService) {
this.orderService = orderService;
this.userService = userService;
}
}
✅ Summary Table
| Way | Use? | Status |
|---|---|---|
| Constructor Injection | ✔ YES | ⭐ Best |
| Lombok Constructor | ✔ YES | ⭐ Best |
| Field Injection | ❌ Avoid | Bad |
| Setter Injection | ⚠ Rare | Special case |
Agar chaho main tumhe next step dikha sakta hoon:
✅ Controller → Service → Repository full example
✅ POST request (DTO → Entity)
✅ Exception handling with service layer
Bas bolo:
👉 "Full flow example dikhao"
ya
👉 "POST request flow dikhao"
Top comments (0)