on
Spring error - The dependencies of some of the beans in the...
Spring error - The dependencies of some of the beans in the...
의존성 순환 참조
순환 참조 에러는 둘 이상의 Bean이 생성자를 통해 빈을 주입받으려고 할 때 생긴다.
securityConfig에 있는 TokenProvider가 AccountService 빈을 주입받게 되고, 또 AccountService에서는 SecurityConfig.passwordEncoder를 참조하고 있어서 계속 순환 참조가 일어나게 되는 것이다.
근데 이 경우에는
SecurityConfig -> TokenProvider -> AccountService 순으로 참조하지만,
TokenProvider는 InitializingBean을 implements 하고 있기 때문에 AccountService가 있지 않은 상황에서 AccountService를 주입받으려고 하다보니 오류가 생기는 것 같았다.
해결 방법
1. USE @LAZY
@Component public class TokenProvider implements InitializingBean { private AccountService accountService; public TokenProvider(@Lazy AccountService accountService) { this.accountService = accountService; } // ... }
@Lazy를 이용하면 일단 Proxy 객체를 주입시켜준 다음에, 실제로 사용할 때 실제 빈을 주입시켜주는 것이다. (지연로딩)
그 외 다른 해결 방법들 아래 참조
https://www.baeldung.com/circular-dependencies-in-spring
from http://iseunghan.tistory.com/379 by ccl(A) rewrite - 2021-11-27 11:27:20