새소식

Welcome to the tech blog of Junior Backend Developer Myoungji Kim!

Development/Spring

[Error] Parameter 0 of constructor in service required a bean of type repository that could not be found.

  • -

📍 배경

Springboot + Kotlin + H2 조합으로 간단한 API 를 구축해보려고 했다.

 

📍 이슈

Error starting ApplicationContext. To display the condition evaluation report re-run your application with 'debug' enabled.
2024-04-11T23:22:48.130+09:00 ERROR 38085 --- [           main] o.s.b.d.LoggingFailureAnalysisReporter   : 

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

Description:

Parameter 0 of constructor in com.example.application.member.MemberService required a bean of type 'com.topda.application.member.MemberRepository' that could not be found.


Action:

Consider defining a bean of type 'com.example.application.member.MemberRepository' in your configuration.


> Task :ApplicationKt.main() FAILED

Deprecated Gradle features were used in this build, making it incompatible with Gradle 9.0.

 

Application Build 과정에서 계속 bean 관련 에러가 발생했다.

그러나 실질적으로 어노테이션으로 Bean을 주입해주어도 이슈는 해결되지 않았기에, 다른 곳에서 원인을 찾아봐야했다.

 

📍해결

R2DBC 종속성 제거하여 해결

 

처음에 spring.io에서 프로젝트 generate 시, 눈에 보이는 대로 이것저것 추가했으나..

R2DBC와 H2에 대해 조금 더 상세히 살펴봤어야 했다.

더보기

📌 개념 알고가기

1. H2 Database

- 자바로 작성된 관계형 데이터베이스 관리 시스템
- 스프링 부트가 지원하는 인메모리 관계형 데이터베이스
- 인메모리로 띄우면 애플리케이션 재기동 때마다 초기화 됨
- 웹용 콘솔(쿼리툴) 제공하여 개발용 로컬DB로 많이 사용됨


2. R2DBC
- Reactive Relational Database Connectivity

- 적은 수의 스레드로 동시성을 처리하며, 더 적은 하드웨어 리소스로 확장 가능한 논블로킹 어플리케이션 스택

 

* 보통 (Spring MVC + JPA), (Spring Webflux + R2DBC) 조합으로 많이 사용함!

 

🔗 관련 StackOverFlow (problem-extending-spring-webflux-application-with-reactive-db-client-conflictin)

 

Problem extending Spring WebFlux application with reactive DB client: conflicting Maven dependencies?

I have a small working Spring WebFlux app made basing on this code: https://github.com/chang-chao/spring-webflux-reactive-jdbc-sample As far as I've understood this is some kind of mix between purely

stackoverflow.com

 

위 스택오버플로우 글에서 나와 똑같은 에러 케이스를 마주한 사람을 발견했다.

질문자는 WebFlux 모듈을 사용하고 있었기에, application.properties 내 설정 key 값을 올바르게 지정해주었어야 했다.

그러나 나는 우선 MVC 모델로 먼저 시작할 예정이기에.. R2DBC 종속성을 제거하는 것으로 결정했다.

 

AS-IS

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-data-r2dbc'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
	implementation 'io.projectreactor.kotlin:reactor-kotlin-extensions'
	implementation 'org.jetbrains.kotlin:kotlin-reflect'
	implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor'
	compileOnly 'org.projectlombok:lombok'
	runtimeOnly 'com.h2database:h2'
	runtimeOnly 'io.r2dbc:r2dbc-h2'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'io.projectreactor:reactor-test'
}

 

TO-BE

dependencies {
	implementation 'org.springframework.boot:spring-boot-starter-data-jdbc'
	implementation 'org.springframework.boot:spring-boot-starter-data-jpa'
	implementation 'org.springframework.boot:spring-boot-starter-web'
	implementation 'com.fasterxml.jackson.module:jackson-module-kotlin'
	implementation 'io.projectreactor.kotlin:reactor-kotlin-extensions'
	implementation 'org.jetbrains.kotlin:kotlin-reflect'
	implementation 'org.jetbrains.kotlinx:kotlinx-coroutines-reactor'
	compileOnly 'org.projectlombok:lombok'
	runtimeOnly 'com.h2database:h2'
	annotationProcessor 'org.projectlombok:lombok'
	testImplementation 'org.springframework.boot:spring-boot-starter-test'
	testImplementation 'io.projectreactor:reactor-test'
}

 

 

R2DBC는 추후 더 상세히 공부해봐야겠다!

 

종속성 제거 후, 어플리케이션을 실행하니 드디어.. 에러가 사라졌다... 꺆!!

 

PHP 하다가, Spring+Kotlin으로 넘어오니 다시 병아리가 된 기분이지만.. 차근차근 잘 해보자!

 

 

Contents

포스팅 주소를 복사했습니다

이 글이 도움이 되었다면 공감 부탁드립니다.