Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: flush clear 기능 annotation 기반 AOP로 전환
- Loading branch information
Showing
3 changed files
with
52 additions
and
4 deletions.
There are no files selected for viewing
14 changes: 14 additions & 0 deletions
14
module-domain/src/main/java/com/yapp/sharefood/annotation/ModifyingQuery.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package com.yapp.sharefood.annotation; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
@Target({ElementType.TYPE, ElementType.METHOD}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ModifyingQuery { | ||
boolean flushAuto() default false; | ||
|
||
boolean cleanAuto() default false; | ||
} |
36 changes: 36 additions & 0 deletions
36
module-domain/src/main/java/com/yapp/sharefood/aop/ModifingQueryAspect.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
package com.yapp.sharefood.aop; | ||
|
||
import com.yapp.sharefood.annotation.ModifyingQuery; | ||
import lombok.RequiredArgsConstructor; | ||
import lombok.extern.slf4j.Slf4j; | ||
import org.aspectj.lang.ProceedingJoinPoint; | ||
import org.aspectj.lang.annotation.Around; | ||
import org.aspectj.lang.annotation.Aspect; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.persistence.EntityManager; | ||
|
||
@Slf4j | ||
@Aspect | ||
@Component | ||
@RequiredArgsConstructor | ||
public class ModifingQueryAspect { | ||
private final EntityManager entityManager; | ||
|
||
@Around("@annotation(modifyingQuery)") | ||
public Object flushAndClearAroundQuery(ProceedingJoinPoint proceedingJoinPoint, ModifyingQuery modifyingQuery) throws Throwable { | ||
if (modifyingQuery.flushAuto()) { | ||
entityManager.flush(); | ||
log.debug("before query flush"); | ||
} | ||
|
||
Object response = proceedingJoinPoint.proceed(); | ||
|
||
if (modifyingQuery.cleanAuto()) { | ||
entityManager.clear(); | ||
log.debug("before query clear"); | ||
} | ||
|
||
return response; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters