관리 메뉴

JIE0025

[에러] The dependencies of some of the beans in the application context form a cycle 해결 본문

백엔드/이슈, 트러블슈팅

[에러] The dependencies of some of the beans in the application context form a cycle 해결

Kangjieun11 2023. 2. 26. 23:55
728x90

Description: The dependencies of some of the beans in the application context form a cycle: answerController defined in file [/Users/kimchangha/Desktop/projects/Section5/pre-project/new/seb42_pre_024/backend/build/classes/java/main/com/codestates_pre024/stackoverflow/answer/controller/AnswerController.class] ┌─────┐ | answerService defined in file [/Users/kimchangha/Desktop/projects/Section5/pre-project/new/seb42_pre_024/backend/build/classes/java/main/com/codestates_pre024/stackoverflow/answer/service/AnswerService.class] ↑ ↓ | questionService defined in file [/Users/kimchangha/Desktop/projects/Section5/pre-project/new/seb42_pre_024/backend/build/classes/java/main/com/codestates_pre024/stackoverflow/question/service/QuestionService.class] └─────┘

 

 

✅  해결

 

이 에러메세지의 핵심은 다음과 같다.

 

┌─────┐

 | answerService //생략

↑ ↓

 | questionService //생략

└─────┘ 

 

 

answerService와 questionService가 서로의 객체를 사용하면서 종속성 순환에 대한 에러를 뱉은 것이다.

 

 

answerService의 필드로 정의되어 사용되는 questionService를 없애던지,

questionService의 필드로 정의되어 사용되는 answerService를 없애던지해야만했다.

 

기획상  answer가 question의 데이터를 찾아야하는데 메서드로 정의되어있음과 동시에

question answer의 데이터를 찾아야하는데 메서드로 정의되어있었어서 

 

팀원 두분이 각자 개발하며 서로의 메서드를 참조해 사용했다가 발생하게 되었다.

 

 

두 분과 많은 대화 끝에

question과 answer는 부모자식 관계에 있었기 때문에 자식인 answer의 코드를 개편하기로 결정했고, 

AnswerService의 필드로 존재하는 questionService를 없앤 후, questionRepository를 사용해 직접 데이터를 가져오도록 수정해주었다.