[Spring]JPA Redis Repository scan 문제

Coodori·2023년 4월 2일
1

CherishU

목록 보기
17/29

문제발생

에러가 나진 않지만 매우매우 불편한 로그가 나를 반겼다.
대충 읽어보면
2023-04-02T18:00:14.921+09:00 INFO 52154 --- [ restartedMain] .RepositoryConfigurationExtensionSupport : Spring Data Redis - Could not safely identify store assignment for repository candidate interface cherish.backend.member.repository.JobRepository; If you want this repository to be a Redis repository, consider annotating your entities with one of these annotations: org.springframework.data.redis.core.RedisHash (preferred), or consider extending one of the following types with your repository: org.springframework.data.keyvalue.repository.KeyValueRepository

저장소 할당을 안전하게 식별할 수 없습니다.작업 리포지토리; 이 리포지토리를 Redis 리포지토리로 만들려면 org.springframework.data.redis.core라는 주석 중 하나로 엔티티에 주석을 달아야 합니다.RedisHash(기본 설정) 또는 저장소를 사용하여 org.springframework.data.keyvalue.repository 유형 중 하나를 확장합니다.

라고 Spring data JPA repository 와 Spirng data redis Repository가 모두 인식되어서 redis가 이게 내 리포지토리가 맞아? 그럼 명시해줘 라는 로그입니다.

문제해결

그렇다면 어떻게 해결해야할까...?

RedisRepository(CRUD repository) 와 Jpa Repository를 모두 사용할 경우
두가지의 data Repository가 등록되는 상황이다.

Spring data 공식 문서

총 등록되는 repository는 총 세개

  • CrudRepository
  • PagingAndSortingRepository
  • JpaRepository

해결 방법 1

그렇다면 기본은 해당 인식을 해주는 클래스를 직접 지정해주면 되었다.
@EnableJpaRepositories
@EnableRedisRepositories
으로 구분하여 사용하는 리포지토리를 명시적 으로 적어주는 것이다.

현재 프로젝트는 계층형을 도메인형으로 나누었기 때문에 repository가 분산되어 있어서 코드에 너무 많은 루트가 적힐 가능성이 컸다.

그렇다면 어떤 방법이 있을까

해결 방법 2

나는 이메일 인증 Redis를 redisTemplate 를 통해 구현을 하였고 redisRepository는 사용하지 않는다.
고로 간편하게

spring:
	data:
    	redis:
			repositories:
			enabled: false

를 설정해주었다.

이렇게 되면 redisRepository를 사용하지않으니 어떤 것을 사용할지 헷갈리지 않고 Jpa Repository만 사용하게 된다.

의문의 한줄

그리고 바로 위에 추가된 의문의 한줄
Bootstrapping Spring Data Redis repositories in DEFAULT mode.

Profile 등 한줄씩 무엇을 뜻하는지는 알겠는데 해당 줄에 대해서는 처음 보아서 조사해보았다.

해당 공식문서

사실 직관적으로 와닿진 않는다.
다른 레퍼런스 문서를 찾아보고 이해한바로는

  • deferred
    Spring Data는 리포지토리를 초기화하고 애플리케이션 시작 전에 포함된 쿼리 및 메타데이터의 유효성을 검사합니다 .
    • 비동기적으로 띄우되 리포지토리에 대해 검사를 해준다.
  • default
    다른 Spring bean과 마찬가지로 주입될 때 초기화가 발생합니다 .
    스프링 생성및 의존 주기에 포함되기 때문에 만약 Repository가 많아지면 로딩이 오래걸린다.
  • lazy
    비동기로 띄워 주되 초기화를 하지 않는다.
    Spring은 테스트에 포함되지 않은 저장소에 포함된 쿼리 및 메타데이터의 유효성을 검사하지 않습니다.
    잠재적인 초기화 오류가 있는 프로덕션 환경에 응용 프로그램을 배포하지 않으려면 개발 중에만 지연 부트스트래핑을 사용해야 합니다

고로 deferred 옵션을 사용했다.

사용법은

spring:
	data:
      jpa:
        repositories:
          bootstrap-mode: deferred

Reference

https://velog.io/@stbpiza/Spring-Boot-JPA-Redis-Repository-INFO

profile
https://coodori.notion.site/0b6587977c104158be520995523b7640

0개의 댓글