File tree Expand file tree Collapse file tree 4 files changed +61
-0
lines changed
src/main/java/velog/soyeon/spring/aop Expand file tree Collapse file tree 4 files changed +61
-0
lines changed Original file line number Diff line number Diff line change
1
+ dependencies {
2
+ implementation ' org.springframework.boot:spring-boot-starter-aop'
3
+ }
Original file line number Diff line number Diff line change
1
+ package velog .soyeon .spring .aop ;
2
+
3
+ import org .springframework .web .bind .annotation .GetMapping ;
4
+ import org .springframework .web .bind .annotation .RequestMapping ;
5
+ import org .springframework .web .bind .annotation .RestController ;
6
+
7
+ @ RestController
8
+ @ RequestMapping ("/aop" )
9
+ public class AopController {
10
+
11
+ // @LogExecutionTime
12
+ @ GetMapping ("/test" )
13
+ public boolean test () throws InterruptedException {
14
+ Thread .sleep (40 );
15
+ return true ;
16
+ }
17
+
18
+ }
Original file line number Diff line number Diff line change
1
+ package velog .soyeon .spring .aop ;
2
+
3
+ import org .aspectj .lang .ProceedingJoinPoint ;
4
+ import org .aspectj .lang .annotation .Around ;
5
+ import org .aspectj .lang .annotation .Aspect ;
6
+ import org .slf4j .Logger ;
7
+ import org .slf4j .LoggerFactory ;
8
+ import org .springframework .stereotype .Component ;
9
+ import org .springframework .util .StopWatch ;
10
+
11
+ @ Aspect // Aspect 명시
12
+ @ Component // 스프링 빈 등록
13
+ public class LogAspect {
14
+
15
+ //로거 생성
16
+ Logger logger = LoggerFactory .getLogger (LogAspect .class );
17
+
18
+ // @Around("@annotation(LogExecutionTime)")
19
+ @ Around ("execution(* velog.soyeon.spring.aop.*.*(..))" ) // 메소드 실행전후에 공통 로직을 적용할 때 사용
20
+ public Object logExecutionTime (ProceedingJoinPoint joinPoint ) throws Throwable {
21
+ StopWatch stopWatch = new StopWatch ();
22
+ stopWatch .start (); // 스탑워치 시작
23
+ Object proceed = joinPoint .proceed (); // 메소드 실행
24
+ stopWatch .stop (); // 스탑워치 끝
25
+ logger .info (stopWatch .prettyPrint ()); // 로깅
26
+ return proceed ;
27
+ }
28
+
29
+ }
Original file line number Diff line number Diff line change
1
+ package velog .soyeon .spring .aop ;
2
+
3
+ import java .lang .annotation .ElementType ;
4
+ import java .lang .annotation .Retention ;
5
+ import java .lang .annotation .RetentionPolicy ;
6
+ import java .lang .annotation .Target ;
7
+
8
+ @ Target (ElementType .METHOD ) // 해당 어노테이션을 어디에서 사용할 수 있을지 결정
9
+ @ Retention (RetentionPolicy .RUNTIME ) // 어노테이션 정보 유지 위치
10
+ public @interface LogExecutionTime {
11
+ }
You can’t perform that action at this time.
0 commit comments