20231103 recording

XingXi·2023년 11월 4일
0

기록

목록 보기
1/33

QueryDsl Test Code 실행 시 다음과 같은 Error 발생

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.project.wemen.member.repository.MemberRepositoryCustom' available: expected single matching bean but found 2: memberRepositoryCustomImpl,memberRepository
	at org.springframework.beans.factory.config.DependencyDescriptor.resolveNotUnique(DependencyDescriptor.java:218) ~[spring-beans-6.0.12.jar:6.0.12]
DependencyDescriptor.java:218
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.doResolveDependency(DefaultListableBeanFactory.java:1395) ~[spring-beans-6.0.12.jar:6.0.12]
DefaultListableBeanFactory.java:1395
	at org.springframework.beans.factory.support.DefaultListableBeanFactory.resolveDependency(DefaultListableBeanFactory.java:1337) ~[spring-beans-6.0.12.jar:6.0.12]
DefaultListableBeanFactory.java:1337

Caused by

Caused by: org.springframework.beans.factory.NoUniqueBeanDefinitionException: No qualifying bean of type 'com.project.wemen.member.repository.MemberRepositoryCustom' available: expected single matching bean but found 2: memberRepositoryCustomImpl,memberRepository
  • NoUniqueBeanDefinitionException : SpringBean Name 이 유효하지 않아서 Error 발생
    - memberRepositoryCustomImpl
    - memberRepository

위의 두개의 class 가 Spring Container 에서 부터 주입 받는데 중복이 발생한다는 것이다

원인

MemberRepositoryCustomImplTest

@SpringBootTest
public class MemberRepositoryCustomImplTest {
    
    @Autowired
    MemberRepositoryCustom memberRepositoryCustom;

    @Test
    void getMemberEntityByAuthLoginId()
    {
        // loginId : practice12
        // member email 
        // input : loginId -> output : memberEntity Instance 
        // checking equalTo memberEntity's email : practice@naver.com

        Optional<MemberEntity> memberEntityList = memberRepository.findByAuthLoginId("practice12");
        Assertions.assertThat(memberEntityList.get().getEmail()).isEqualTo("practice@naver.com");
    }
}

MemberRepository

public interface MemberRepository extends JpaRepository<MemberEntity,Long>, MemberRepositoryCustom
{
    
}

MentorRepositoryCustom

public interface public class MemberRepositoryCustomImpl implements MemberRepositoryCustom
{
    
}

MemberRepositoryCustomImplTest 에서

    @Autowired
    MemberRepositoryCustom memberRepositoryCustom;

를 통해서 MemberRepositoryCustom을 어노테이션 주입 받고 있는 상황인데

MemberRepositoryCustom 구현체가 MemberRepository, MemberRepositoryCustomImpl 이 동시에 구현하고 Container 에 등록되어 이같은 Error 가 발생했다.

해결책은 간단하다.

    @Autowired
    MemberRepositoryCustom memberRepositoryCustom;

대신

    @Autowired 
    MemberRepository memberRepository;

사용하면 된다.

QueryDsl 을 어깨너머로 배우다 보니 이런 기초적인 에러를 발생시킨다. 공부가 필요해보인다!

0개의 댓글