Solving Circular Dependencies: @Lazy Is Just a Workaround
Solving Circular Dependencies: @Lazy Is Just a Workaround
Situation
While implementing the Gift API at Kakao Tech Campus, a circular dependency occurred.
1
MemberService → WishlistService → MemberService
Spring threw BeanCurrentlyInCreationException.
Quick Fix: @Lazy
First attempt was using @Lazy annotation.
1
2
3
4
5
6
7
8
@Service
public class WishlistService {
private final MemberService memberService;
public WishlistService(@Lazy MemberService memberService) {
this.memberService = memberService;
}
}
This delays MemberService bean creation until actually used, resolving the circular reference error.
Mentor Feedback
“@Lazy only postpones the problem. The design issue of circular dependencies still exists. Over time, adding new features becomes harder due to the tangled dependencies.”
Root Cause
The problem was that MemberService and WishlistService both depended on each other.
1
2
MemberService: getMemberWithWishlist() → needs WishlistService
WishlistService: getWishlistWithMember() → needs MemberService
Real Solution: Rethink the Design
- Extract common logic: Move shared logic to a new service
- Use DTOs for data transfer: Don’t pass entire service, pass only needed data
- Apply Facade pattern: Create a coordinating service for cross-service operations
1
2
3
4
5
6
7
8
9
10
11
12
13
// Before: MemberService directly calls WishlistService
// After: MemberWishlistFacade coordinates both
@Service
public class MemberWishlistFacade {
private final MemberService memberService;
private final WishlistService wishlistService;
public MemberWithWishlistDto getMemberWithWishlist(Long memberId) {
Member member = memberService.findById(memberId);
List<WishlistItem> wishlist = wishlistService.findByMemberId(memberId);
return new MemberWithWishlistDto(member, wishlist);
}
}
Lessons Learned
@Lazyis a workaround, not a solution- Circular dependencies indicate design problems
- When they occur, reconsider responsibility distribution
- Proper layering is important
From Kakao Tech Campus 3rd cohort Gift API clone coding, summarizing mentor feedback.
This post is licensed under CC BY 4.0 by the author.