Java & Kotlin/Spring
[Spring Boot] 특정 패키지 내부의 원하는 클래스에 특정 메소드만 지정하여 Aspect 적용
전봉근
2020. 6. 6. 01:39
반응형
특정 패키지 내부의 원하는 클래스에 특정 메소드만 지정하여 Aspect 적용
- 의존성 추가 [build.gradle]
... implementation 'org.springframework.boot:spring-boot-starter-aop' ...
- Bean 등록 [Application.java]
@Bean public SelectLogAspect selectLogAspect() { return new SelectLogAspect(); }
- Controller 생성 [CrudController.java]
import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import org.springframework.web.bind.annotation.*; @RestController @RequestMapping("v1/crud") @AllArgsConstructor public class CrudController { private final CrudService crudService; @ApiOperation("Request Get API") @GetMapping("examples") public String getCallMethod() { return crudService.getCallMethod(); } @ApiOperation("Request Post API") @PostMapping("examples") public String setCallMethod() { return crudService.setCallMethod(); } @ApiOperation("Request Put API") @PutMapping("examples") public String putCallMethod() { return crudService.putCallMethod(); } @ApiOperation("Request Patch API") @PatchMapping("examples") public String patchCallMethod() { return crudService.patchCallMethod(); } @ApiOperation("Request Del API") @DeleteMapping("examples") public String delCallMethod() { return crudService.delCallMethod(); } }
- Service 생성 [CrudService.java]
import org.springframework.stereotype.Service; @Service public class CrudService { public String getCallMethod() { return "Request Get"; } public String setCallMethod() { return "Request Post"; } public String putCallMethod() { return "Request Put"; } public String patchCallMethod() { return "Request Patch"; } public String delCallMethod() { return "Request Delete"; } }
- Aspect 생성 (원하는 CrudService.java의 메소드만 Aspect 적용) [CrudService.java]
import lombok.extern.slf4j.Slf4j; import org.aspectj.lang.ProceedingJoinPoint; import org.aspectj.lang.Signature; import org.aspectj.lang.annotation.Around; import org.aspectj.lang.annotation.Aspect; import org.aspectj.lang.annotation.Pointcut; @Slf4j @Aspect public class SelectLogAspect { @Pointcut("execution(* com.example.bkjeon.base.services.api.v1.crud.CrudService.getCallMethod(..))") public void getCallMethod(){} @Pointcut("execution(* com.example.bkjeon.base.services.api.v1.crud.CrudService.setCallMethod(..))") public void setCallMethod(){} @Pointcut("execution(* com.example.bkjeon.base.services.api.v1.crud.CrudService.putCallMethod(..))") public void putCallMethod(){} @Pointcut("execution(* com.example.bkjeon.base.services.api.v1.crud.CrudService.patchCallMethod(..))") public void patchCallMethod(){} @Pointcut("execution(* com.example.bkjeon.base.services.api.v1.crud.CrudService.delCallMethod(..))") public void delCallMethod(){} @Around("getCallMethod() || setCallMethod() || putCallMethod() || patchCallMethod() || delCallMethod()") public Object outputCrudServiceLogging(ProceedingJoinPoint proceedingJoinPoint) { Object result = null; try { Signature signature = proceedingJoinPoint.getSignature(); long start = System.currentTimeMillis(); result = proceedingJoinPoint.proceed(); long end = System.currentTimeMillis(); log.info("------------ SelectLogAspect Request Service Method: {}", signature.toShortString()); log.info("------------ SelectLogAspect Request Service Method Execution Time: {}", (end - start)); } catch (Throwable throwable) { if (log.isErrorEnabled()) { log.error("------------ SelectLogAspect outputCrudServiceLogging(Aspect) ERROR !! {}", throwable.getMessage()); } } return result; } }
반응형