[Spring] 순환 참조 에러

yoon·2024년 5월 3일

spring-boot

목록 보기
30/41
post-thumbnail

❌ 에러상황

Action:
Relying upon circular references is discouraged and they are prohibited by default. Update your application to remove the dependency cycle between beans. As a last resort, it may be possible to break the cycle automatically by setting spring.main.allow-circular-references to true.

✔ 원인

sprig-boot의 버전이 올라가면서 순환 참조가 금지되었다.

나의 상황

// jwtUtil
public clss JwtUtil{
	private final UserService userService;
    ...
    
    UserDetails userDetails = userService.loadUserByUsername(username);

}
//userService
public class UserService implements UserDetailsService{
	private final JwtUtil jwtUtil;

}

처음에 userService를 만들때 UserDetailService를 상속받아 사용하고 있었다.
그래서 jwtUtil에서 UserDetails를 얻기 위해 userService bean이 생성되어 있어야하는데 그러기 위해서는jwtUtil bean이 생성되어 있어야했다.

즉!! jwtUtil > userService > jwtUtil > ... 무한 반복

✔ 해결방법

순환 구조를 끊어주면 된다.
userServiceuserDetailsServiceImpl로 나누어 상속을 옮겨주었다.

참조
https://velog.io/@platinouss/Spring-Circular-References-%ED%95%B4%EA%B2%B0%ED%95%98%EA%B8%B0

profile
하루하루 차근차근🌱

0개의 댓글