Spring Boot Error) Parameter 0 of constructor in ~~~ required a bean of type '~~~' that could not be found.

 

Error

지정한 리파지토리를 찾을 수 없다고 나옴

Description:

Parameter 0 of constructor in ~~~ required a bean of type '~~~' that could not be found.


Action:

Consider defining a bean of type '~~~' in your configuration.

해결 방법

리파지토리가 같은 경로에 있지 않아서 찾지 못하는 경우이다.

불러오는 클래스 위에 애너테이션을 추가해 직접 경로를 잡아준다.


방법1: ComponentScan

그렇지만 해당 경로를 입력해도 내 경우엔 해결되지 않았다.

@ComponentScan(basePackages = {"패키지경로"})

방법2 : EnableJpaRepositories

내 경우엔 JpaRepository를 사용했었다.

아래와 같이 입력을 하니 문제가 해결되었으나 또 다른 오류가 생겼다.

@EnableJpaRepositories(basePackages = {"패키지경로"})

Error

엔티티를 찾을 수 없음

Caused by: java.lang.IllegalArgumentException: Not a managed type: class ~~~

 

해결 방법

엔티티 또한 직접 경로를 입력해준다.

@EntityScan(basePackages = {"패키지 경로"})

작동 확인

프로젝트 디렉토리 구조

메인 컨트롤러와 데이터를 다루는 엔티티와 리파지토리가 다른 경로에 있음

 

컨트롤러

EnableJpaRepositories와 EntityScan 애너테이션 입력


실행

이상없이 잘 실행 된다!


출처

https://github.com/HomoEfficio/dev-tips/blob/master/Spring%20-%20Autowiring%20%EA%B4%80%EB%A0%A8%20%EC%98%A4%EB%A5%98.md

 

GitHub - HomoEfficio/dev-tips: 개발하다 마주쳤던 작은 문제들과 해결 방법 정리

개발하다 마주쳤던 작은 문제들과 해결 방법 정리. Contribute to HomoEfficio/dev-tips development by creating an account on GitHub.

github.com