[스프링] Field itemRepository in com.dreamaz.gongga.item.services.ItemService required a bean of type 'com.dreamaz.gongga.item.repository.ItemRepository' that could not be found.

프로그래밍/서버2021. 3. 30. 17:15

문제 현상

spring boot의 버전을 2.0.3에서 2.4.4로 올리니 여러 문제들이 발생했습니다.

 

그 중 하나는 maven compile 후 실행을 시키면 곧 바로 아래의 에러가 발생하는 것이었습니다.

Field itemRepository in com.dreamaz.gongga.item.services.ItemService required a bean of type 'com.dreamaz.gongga.item.repository.ItemRepository' that could not be found.

The injection point has the following annotations:
	- @org.springframework.beans.factory.annotation.Autowired(required=true)


Action:

Consider defining a bean of type 'com.dreamaz.gongga.item.repository.ItemRepository' in your configuration.


Process finished with exit code 1

 

원인

@Repository
interface ItemRepository : CrudRepository<Item, String>

ItemRepository에 @Repository 어노테이션이 붙어있음에도 스프링 부트 버전을 올린 후 제대로 찾지를 못하고 있습니다.

 

Application 클래스는 package com.dreamaz.gongga.item에 있고

나머지 클래스들은 다 package com.dreamaz.gongga.item 하위 패키지에 있어서 자동으로 component scan이 되어야하는데 찾지 못하고 있습니다.

 

스프링 부트 버전 별로 테스트하고, 스프링 부트 소스를 다운받아 스프링 부트에 존재하는 모듈을 버전별로 달리 태그/커밋을 변경하면서 빌드/테스트 하여 이 문제가 아래의 커밋과 관련이 있다는 것을 찾았습니다. (원인 찾는데 하루종일 걸림 ㅜㅜ)

github.com/spring-projects/spring-boot/commit/2147976c178a4eab7ca02ba99cfb53d767e3d8f1#diff-49d8a03f18a0c5bbaf37f1959a4a060cdc4f56c6c53e284113a9c20304705029

 

Do not fallback to embedded configuration if a datasource url is set

This commit makes sure that a fallback embedded datasource is not created if no suitable connection pool is found and an url has been explicitly registered. This is consistent with EmbeddedDataSourceConfiguration as it is using EmbeddedDatabaseBuilder behind the scenes and the latter does not honour the configured URL anyway.

 

이 커밋은 스프링 부트 2.2.5 버전부터 포함됐으며 spring-boot-autoconfigure 모듈입니다.

 

또한 이 부분은 제 pom의 아래 설정과 관련이 있습니다.

어떤 테스트를 한다고 HikariCP를 제외시켰었습니다.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
            <exclusions>
                <exclusion>
                    <groupId>com.zaxxer</groupId>
                    <artifactId>HikariCP</artifactId>
                </exclusion>
            </exclusions>
        </dependency>

 

 

해결 방법

HikariCP을 제외시키는 코드를 제거해주면 됩니다.

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
<!--            <exclusions>
                <exclusion>
                    <groupId>com.zaxxer</groupId>
                    <artifactId>HikariCP</artifactId>
                </exclusion>
            </exclusions>-->
        </dependency>

 

 

 

참고

stackoverflow.com/questions/55750957/why-is-this-componentscan-not-working-in-my-spring-boot-application

 

Why is this @ComponentScan not working in my spring boot application?

I am learning spring boot. This is my spring boot project structure. I do intentionally keep my application.java in a different package to learn about @ComponentScan Project source - https://githu...

stackoverflow.com

docs.spring.io/spring-boot/docs/current/reference/html/using-spring-boot.html#using-boot-structuring-your-code

 

Using Spring Boot

This section goes into more detail about how you should use Spring Boot. It covers topics such as build systems, auto-configuration, and how to run your applications. We also cover some Spring Boot best practices. Although there is nothing particularly spe

docs.spring.io

github.com/spring-projects/spring-boot/issues/19603

 

Consider defining a bean of type 'com.xxx.ElasticsearchRestClient' in your configuration · Issue #19603 · spring-projects/spri

Affects: Spring Framework version 5.1.10 & spring boot version 2.1.9 hello , my project use spring boot version 2.1.9 , Spring Framework version is 5.1.10 my class : @Component public class Ela...

github.com

stackoverflow.com/a/47101828/7225691

 

How to display auto-configuration report when running a Spring Boot application

Error starting ApplicationContext. To display the auto-configuration report re-run your application with 'debug' enabled I am getting the above message when I try to run my Spring Boot applicati...

stackoverflow.com

docs.spring.io/spring-boot/docs/current/reference/html/production-ready-features.html

 

Spring Boot Actuator: Production-ready Features

HTTP Tracing can be enabled by providing a bean of type HttpTraceRepository in your application’s configuration. For convenience, Spring Boot offers an InMemoryHttpTraceRepository that stores traces for the last 100 request-response exchanges, by default

docs.spring.io

 

작성자

Posted by 드리머즈

관련 글

댓글 영역