일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | 6 | 7 |
8 | 9 | 10 | 11 | 12 | 13 | 14 |
15 | 16 | 17 | 18 | 19 | 20 | 21 |
22 | 23 | 24 | 25 | 26 | 27 | 28 |
29 | 30 | 31 |
Tags
- ubuntu
- Spring
- Git
- IntelliJ
- devops
- Oracle
- linux
- JVM
- 맛집
- it
- Design Patterns
- springboot
- java
- jenkins
- AWS
- tool
- Spring Boot
- ReactJS
- laravel
- redis
- Gradle
- php
- Spring Batch
- elasticsearch
- MySQL
- db
- javascript
- 요리
- jsp
- Web Server
Archives
- Today
- Total
아무거나
[Spring Boot] 특정 패키지 내부의 원하는 클래스에 특정 메소드만 지정하여 Aspect 적용 본문
반응형
특정 패키지 내부의 원하는 클래스에 특정 메소드만 지정하여 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; } }
반응형
'Java & Kotlin > Spring' 카테고리의 다른 글
[Lombok] Lombok 사전 (0) | 2020.11.27 |
---|---|
Field Injection이 아닌 Constructor Injection 사용하자 (0) | 2020.11.24 |
[Spring Boot] Spring Boot + Mybatis + Mysql 을 활용한 계층형(=hierarchy) 게시판 구현 (0) | 2020.05.31 |
[Spring Boot] *Service로 끝나는 파일만 접근하는 Aspect 적용 (0) | 2020.05.16 |
Log4J 에서 isDebugEnabled() 을 사용한 효율적인 리소스 관리 (0) | 2020.05.08 |
Comments