-
[spring boot] 'Parameter 0 of constructor in ~ ', 'Consider defining a bean of type ~' 오류 해결spring 2024. 11. 20. 11:54
목차
- 오류내용
- 오류 내용 해석
- 문제해결
1) jpaRepository에 이상 없는지 점검하기
2) 연결한 데이터베이스에 이상 없는지 점검하기
오류 내용
*************************** APPLICATION FAILED TO START *************************** Description: Parameter 0 of constructor in test.testProject.service.PrincipalDetailsService required a bean of type 'test.testProject.repository.UserRepository' that could not be found. Action: Consider defining a bean of type 'test.testProject.repository.UserRepository' in your configuration.
오류 내용 해석
특정 클래스의 bean을 찾을 수 없다는 오류다. 어떤 이유로 인해 bean 등록이 되지 않아 오류가 발생했다.
문제 해결
문제의 클래스가 repository인 경우 아래 방법을 확인해보자!
1) jpaRepository에 이상 없는지 점검하기
문제의 repository에 jpaRepository를 상속받은 상태였는데, jpaRepository를 상속받지 않으면 오류가 나지 않았다. 그래서 jpa 관련 오류일거라 생각했다.
- repository와 entity가 잘 연결되어있는지 확인
- gradle에 아래코드가 있는지 확인
implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
- webflux같은 다른 충돌은 없는지 확인
모두 확인했지만 이상이 없었다.
2) 연결한 데이터베이스에 이상 없는지 점검하기
jpaRepository는 연결된 데이터베이스가 정상 작동하지 않으면 bean으로 등록되지 않는다고 한다. 그래서 데이터베이스도 점검해보았다. 데이터베이스는 H2를 사용하였다.
- 데이터베이스 설정 제대로 적었는지 확인, 데이터베이스가 실행중인지 확인
application.yaml에 아래 코드같은 데이터베이스를 설정해야 한다.h2의 경우 예시
spring.datasource.url=jdbc:h2:mem:testdb spring.datasource.driver-class-name=org.h2.Driver spring.datasource.username=sa spring.datasource.password= spring.jpa.hibernate.ddl-auto=update
- 아래 코드 존재 여부 확인
application.yaml에 아래 코드가 있으면 주석처리해야 한다!
spring: autoconfigure: exclude: org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration
이 코드는 데이터베이스를 설정하지 않았을때 오류없이 실행되도록 하는 코드다. 개발중에 데이터베이스를 아직 마련하지 않았을때 유용하다.
그런데 데이터베이스가 있는데도 해당 코드를 작성해놓으면 문제가 발생한다. 해당 코드가 있으면 데이터베이스를 사용하는 모듈(Spring Data JPA 등)이 정상적으로 동작하지 않는다. 나의 경우 이 부분때문에 에러가 발생했다.
'spring' 카테고리의 다른 글
intelliJ에서 .env 파일 적용법 (1) 2025.01.29 [spring boot] 도커 사용시 DevTools 적용하기 (0) 2025.01.05 [spring boot] QueryDSL 세팅하기 (0) 2025.01.03 react와 spring boot 간 연동하기 / 통신하기 (0) 2024.11.07 spring yml/yaml/properties 파일 작동 안됨 오류 해결법 (0) 2024.11.06