인프런 영문 브랜드 로고
인프런 영문 브랜드 로고

인프런 커뮤니티 질문&답변

gusdn85554님의 프로필 이미지
gusdn85554

작성한 질문수

스프링 DB 2편 - 데이터 접근 활용 기술

Querydsl 강의와 연관된 질문

작성

·

333

1

안녕하세요 영한님, 서포터즈님들

저는 영한님 Querydsl 강의를 다 듣고 나서 현재 강의를 듣고 있는데, 현재 강의의 querydsl 의 클래스 의존관계와 Querydsl 강의의 클래스 의존관계가 달라 궁금한 점이 생겼습니다.

기존 Querydsl 강의에서는 아래의 사진과 같은 클래스 의존관계로 만들었습니다.

public interface ItemRepositoryV3 extends JpaRepository<Item, Long>, ItemRepositoryCustom {
}

public interface ItemRepositoryCustom {
}

public class ItemRepositoryImpl implements ItemRepositoryCustom {
}

@RequiredArgsConstructor
@Service
public class ItemServiceV3 {

    private final ItemRepositoryV3 itemRepositoryV3;
}

ItemServiceV3에서는 ItemRepositoryV3를 주입받는데 이때 구현체는 어떤 것인지 궁금해서 코드를 작성해보았습니다.

@SpringBootTest
class ItemServiceV3Test {

    @Autowired
    ItemServiceV3 itemServiceV3;

    @Autowired
    ItemRepositoryV3 itemRepositoryV3;

    @Autowired
    ItemRepositoryCustom itemRepositoryCustom;

    @Test
    void aa() throws Exception {
        System.out.println("itemServiceV3.getClass() = " + itemServiceV3.getClass());
        System.out.println("itemRepositoryV3.getClass() = " + itemRepositoryV3.getClass());
        System.out.println("itemRepositoryCustom.getClass() = " + itemRepositoryCustom.getClass());


        System.out.println("ProxyUtils.getUserClass(itemRepositoryV3) = " + ProxyUtils.getUserClass(itemRepositoryV3));
        System.out.println("ProxyUtils.getUserClass(itemRepositoryCustom) = " + ProxyUtils.getUserClass(itemRepositoryCustom));
    }

}

itemServiceV3.getClass() = class hello.itemservice.repository.v3.ItemServiceV3

itemRepositoryV3.getClass() = class com.sun.proxy.$Proxy106

itemRepositoryCustom.getClass() = class com.sun.proxy.$Proxy106

ProxyUtils.getUserClass(itemRepositoryV3) = class org.springframework.data.jpa.repository.support.SimpleJpaRepository

ProxyUtils.getUserClass(itemRepositoryCustom) = class org.springframework.data.jpa.repository.support.SimpleJpaRepository

 

그래서 위와 같은 결과를 얻을 수 있었는데요

여기서 궁금한 점이 ItemRepositoryCustom 인터페이스는 아무런 제스처도 취하지 않았는데 왜 프록시 클래스가 만들어진건지 모르겠습니다..,,

제가 Querydsl 강의에서 놓친 부분이 있다면 어느 부분인지 말씀해주시면 감사하겠습니다.

긴 글 읽어주셔서 감사합니다.

 

 

답변 1

1

김영한님의 프로필 이미지
김영한
지식공유자

안녕하세요. gusdn85554님

ItemRepositoryCustom의 구현체 코드를 만들어두었어도 누군가는 ItemRepositoryCustom의 구현체를 호출하도록 연결을 해주어야겠지요?

ItemRepositoryCustom의 구현체를 연결하는 코드가 결국 필요한데, 스프링 데이터 JPA가 자동으로 프록시를 만들어서 해결해줍니다.

ItemRepositoryCustom의 기능을 사용하게 되면 다음과 같이 동작합니다.

클라이언트 -> Proxy -> ItemRepositoryCustom구현체

감사합니다.

gusdn85554님의 프로필 이미지
gusdn85554
질문자

안녕하세요 영한님

주말에도 답변 감사드립니다

생각해보니 ItemRepositoryCustomImpl를 빈으로 등록하지 않았는데 의존관계 주입이 당연히 될수가 없겠네요,, 이 부분을 간과했던 것 같습니다

그러면 결국 런타임에는 현재 ItemRepositoryCustom 에 주입된 SimpleJpaRepository가 연결해준다고 생각하면 될까요,,?

어느 부분에서 연결을 해주는지 궁금한데,, 혹시 키워드를 알 수 있을까요?

감사합니다.

김영한님의 프로필 이미지
김영한
지식공유자

안녕하세요. gusdn85554님

프록시 클래스가 하나 들어오는데요. 여기에서 호출해준다고 보시면 됩니다. 관련해서 따로 설명된 곳은 없어서, 디버깅을 키고 따라가보셔야 할 것 같아요^^

감사합니다.

gusdn85554님의 프로필 이미지
gusdn85554

작성한 질문수

질문하기