SH380 Logo
2025-08-19

Springboot 순환참조 문제

#Springboot#server#Troubleshooting

springboot-logo

***************************
APPLICATION FAILED TO START
***************************

Description:

The dependencies of some of the beans in the application context form a cycle:

   loginController defined in file [C:\github\Dotree-BE\dotree\build\classes\java\main\com\project\dotree\app\controller\LoginController.class]
┌─────┐
|  securityConfig defined in file [C:\github\Dotree-BE\dotree\build\classes\java\main\com\project\dotree\global\config\SecurityConfig.class]
↑     ↓
|  customOAuth2SuccessHandler defined in file [C:\github\Dotree-BE\dotree\build\classes\java\main\com\project\dotree\app\oauth2\CustomOAuth2SuccessHandler.class]
└─────┘

프로젝트를 진행하던 중 순환 참조 문제가 발생했다.
순환 참조 문제에 대해서 알아보자.

문제 원인
loginController
 ↓ (PasswordEncoder 사용을 위해 SecurityConfig 주입)
securityConfig
 ↓ (CustomOAuth2SuccessHandler @Bean 등록)
customOAuth2SuccessHandler
 ↓ (SecurityConfig 내부에서 @Bean으로 정의됨)
securityConfig (다시 필요함 -> 순환 발생)

해결 방법

Bean (빈)

정상적인 Bean 생성 & 생성자 주입

[LoginService]  <-- 생성자 주입
      │
      ▼
[PasswordEncoder Bean]  (스프링이 관리)

순환 참조 발생 예시

[A Bean]  <-- 생성자 주입
   │
   ▼
[B Bean]  <-- 생성자 주입
   │
   ▼
[A Bean]  (다시 필요)  <-- 무한 루프

A를 만들려면 B가 필요
B를 만들려면 A가 필요
스프링이 어디서부터 만들지 몰라서 에러 발생 → 순환참조 발생

목록으로 돌아가기