From 0706db9d1d5194c091e518b701a715334911a5b4 Mon Sep 17 00:00:00 2001 From: Hyeon-Uk Date: Fri, 16 Aug 2024 09:19:11 +0900 Subject: [PATCH] =?UTF-8?q?[refactor]=20JPAQueryFactory=EB=A5=BC=20BeanCon?= =?UTF-8?q?tainer=EB=A1=9C=20=EC=83=9D=EC=84=B1=20=ED=9B=84=20=EA=B4=80?= =?UTF-8?q?=EB=A6=AC=ED=95=98=EB=8F=84=EB=A1=9D=20=EB=B3=80=EA=B2=BD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - 기존 로직은 메서드에서 매번 팩토리를 생성 - 매번 메서드마다 팩토리를 생성하는 코드 중복이 일어남 - 팩토리를 매번 생성하면 성능적인 오버헤드가 생김 - 팩토리는 Thread-Safe한 객체기 때문에 싱글톤으로 관리되어도 아무런 문제가 없다. --- .../woowak/lab/web/config/QuerydslConfig.java | 16 ++++++++++++++++ .../camp/woowak/lab/web/dao/store/StoreDao.java | 7 ++++--- 2 files changed, 20 insertions(+), 3 deletions(-) create mode 100644 src/main/java/camp/woowak/lab/web/config/QuerydslConfig.java diff --git a/src/main/java/camp/woowak/lab/web/config/QuerydslConfig.java b/src/main/java/camp/woowak/lab/web/config/QuerydslConfig.java new file mode 100644 index 00000000..fb6d32a1 --- /dev/null +++ b/src/main/java/camp/woowak/lab/web/config/QuerydslConfig.java @@ -0,0 +1,16 @@ +package camp.woowak.lab.web.config; + +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; + +import com.querydsl.jpa.impl.JPAQueryFactory; + +import jakarta.persistence.EntityManager; + +@Configuration +public class QuerydslConfig { + @Bean + public JPAQueryFactory jpaQueryFactory(EntityManager entityManager) { + return new JPAQueryFactory(entityManager); + } +} diff --git a/src/main/java/camp/woowak/lab/web/dao/store/StoreDao.java b/src/main/java/camp/woowak/lab/web/dao/store/StoreDao.java index 9923d30e..92712fb9 100644 --- a/src/main/java/camp/woowak/lab/web/dao/store/StoreDao.java +++ b/src/main/java/camp/woowak/lab/web/dao/store/StoreDao.java @@ -16,11 +16,12 @@ @Repository @Transactional(readOnly = true) public class StoreDao { - @PersistenceContext - private EntityManager entityManager; + private final JPAQueryFactory qf; + public StoreDao(JPAQueryFactory qf) { + this.qf = qf; + } public StoreInfoResponse findAllStoreList() { - JPAQueryFactory qf = new JPAQueryFactory(entityManager); QStore store = QStore.store; List fetchResult = qf.select(store)