Parameter 0 of constructor in...

Godtaek·2023년 11월 5일
0

Spring

목록 보기
1/9

에러코드

Parameter 0 of constructor in {Service} required a bean of type {Repository} that could not be found

에러원인

  1. Service를 빈 객체로 등록할 때, Repository 빈 객체를 주입받아야 되는데, Repository 빈 객체가 존재하지 않음.

    그래서 Spring에서 제안한 해결책
    Consider defining a bean of type {Repository} in your configuration.
    즉 Repository를 빈 객체로 등록해라

  2. 그러나 Repository에 @Repository 어노테이션을 통해서 빈 객체 등록함. 컴포넌트 스캔으로 등록되어야 하지만, 등록이 안 되고 있는 상황

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

DataSourceAutoConfiguration를 제외해서 Repository가 빈 등록되지 않았던 것이었음.

해결

@SpringBootApplication(exclude = DataSourceAutoConfiguration.class)

-> 

@SpringBootApplication()

exclude 문을 삭제하고, db를 연결했더니 정상작동했다.

exclude문이 있을 때, Service, Controller는 빈 객체가 정상 등록되었지만, Repository는 빈 객체 등록이 안 되었던 것을 확인했다.

사유는 모르겠다. Repository 어노테이션을 Service 등으로 수정해도 빈 객체 생성이 되지 않았다. Repository가 DB에 접근하는 코드니까 제외한다고 생각했는데...

결론적으론, DataSource 접근이 되지 않아 @Repository 스캔이 안 될 수 있다고 한다.

만약 다음과 같은 상황이 생긴다면,

@EnableJpaRepositories(basePackages = "com.example.ABC")

어노테이션을 사용하여 수동으로 스캔 설정해줄 수 있다고 한다.

profile
성장하는 개발자가 되겠습니다

0개의 댓글