[SpringBoot][오류해결] The dependencies of some of the beans in the application context form a cycle

2022. 2. 26. 18:00·JAVA/오류해결

 

구글 OAuth를 사용하던 도중 오류가 발생했다. 

APPLICATION FAILED TO START. 앱이 실행하는 것에 실패했다고 나온다. 오류 내용을 확인해보자.

 

 

- 오류 내용

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

Description: 

The dependencies of some of the beans in the application context form a cycle: 
┌─────┐ 
| securityConfig (field private com.cos.security1.config.oauth.PrincipalOauth2UserService com.cos.security1.config.SecurityConfig.principalOauth2UserService) 
↑    ↓ 
| principalOauth2UserService (field private org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder com.cos.security1.config.oauth.PrincipalOauth2UserService.bCryptPasswordEncoder) 
└─────┘ 

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.

오류 내용을 살펴보면 의존성 문제 중에서도 순환 참조 문제가 발생한 것 같다. 순환 참조 문제란 둘 이상의 Bean이 생성자를 통해 서로를 주입되도록 구성하면 Spring IoC 컨테이너에서 감지하는 문제이다.

Description에 나온 화살표도 표시된 그림을 보면 대충 알 수 있는데, 순환참조의 오류가 발생하여 securityConfig와 principalOauth2UserService가 서로를 참조하고 있어 어떤 객체를 먼저 생성할지 문제가 발생한 것이다.

 

 

 

오류 코드

@Service
public class PrincipalOauth2UserService extends DefaultOAuth2UserService {
	
    // 문제의 코드 - 제거해주면 해결
	@Autowired
	private BCryptPasswordEncoder bCryptPasswordEncoder;
	
    ...
}

 

public class SecurityConfig extends WebSecurityConfigurerAdapter{

	@Autowired
	private PrincipalOauth2UserService principalOauth2UserService; 
	
	@Bean
	public BCryptPasswordEncoder encodePwd() {
		return new BCryptPasswordEncoder();
	}
}

SecurityConfig는 PrincipalOauth2UserService 자체를 참조하고, PrincipalOauth2UserService는 SecurityConfig에 있는BCryptPasswordEncoder 클래스를 참조하고 있다. 이것이 순환참조의 원인이다. 굳이 필요없는 클래스라서 제거해줬다.

BCryptPasswordEncoder가 굳이 필요하지 않았기에 제거해주면 오류가 해결된다.

 

 

 

'JAVA/오류해결' 카테고리의 다른 글
  • java.lang.SecurityException: class "org.hamcrest.Matchers"'s signer information does not match signer information of other classes in the same package
  • [Spring Boot][오류해결] Modifying queries can only use void or int/Integer as return type
  • [Spring Boot][오류해결] Executing an update/delete query
  • [Spring Boot][오류해결] Parameter 0 of constructor in ~~ required a bean of type ~~ that could not be found.
gakko
gakko
좌충우돌 개발기
  • gakko
    MYVELOP 마이벨롭
    gakko
  • 전체
    오늘
    어제
    • 분류 전체보기 (205)
      • Spring (23)
        • Spring (10)
        • Spring Boot (7)
        • Spring Security (1)
        • Hibernate (4)
      • Test (3)
      • 끄적끄적 (6)
      • 활동 (35)
        • 부스트캠프 (23)
        • 동아리 (3)
        • 컨퍼런스 (3)
        • 글또 (5)
        • 오픈소스 컨트리뷰션 (1)
      • 디자인패턴 (0)
      • Git & GitHub (22)
        • Git (13)
        • Github Actions (1)
        • 오류해결 (5)
        • 기타(마크다운 등) (3)
      • 리눅스 (6)
        • 기초 (6)
        • 리눅스 서버 구축하기 (0)
      • Infra (2)
        • Docker (1)
        • Elastic Search (0)
        • Jenkins (1)
        • AWS (1)
      • MySQL (7)
        • 기초 (6)
        • Real MySQL (1)
      • 후기 (3)
        • Udemy 리뷰 (3)
      • CS (26)
        • 웹 기본지식 (0)
        • 자료구조 (13)
        • 운영체제 OS (12)
        • 데이터베이스 (1)
        • 시스템 프로그래밍 (0)
        • 기타 (0)
      • Tools (1)
        • 이클립스 (1)
        • IntelliJ (0)
      • 프로젝트 (2)
        • 모여모여(부스트캠프) (1)
      • JAVA (32)
        • Maven (6)
        • 오류해결 (11)
        • 자바 클래스&메소드 (1)
        • JSP & Servlet (12)
      • Javascript (5)
        • 기초 (3)
        • React (2)
      • Python (28)
        • 파이썬 함수 (9)
        • 알고리즘 문제풀이 (16)
        • 데이터 사이언스 (2)
        • 웹 크롤링 (1)
      • 단순정보전달글 저장소 (0)
  • 블로그 메뉴

    • 홈
    • 태그
    • 방명록
  • 링크

    • 우진님
  • 공지사항

  • 인기 글

  • 태그

    Python
    jsp
    알고리즘
    자바스크립트
    부스트캠프
    스프링
    웹개발
    os
    스프링부트
    오류해결
    부스트캠프 멤버십
    java
    자바
    GitHub
    Git
    운영체제
    부스트캠프 7기
    Spring
    파이썬
    MySQL
  • 최근 댓글

  • hELLO· Designed By정상우.v4.10.0
gakko
[SpringBoot][오류해결] The dependencies of some of the beans in the application context form a cycle
상단으로

티스토리툴바