Skip to content

Commit 0cd3420

Browse files
committed
Feat: AOP 예제 추가 (#10)
1 parent 72cc732 commit 0cd3420

File tree

4 files changed

+61
-0
lines changed

4 files changed

+61
-0
lines changed

spring-test-server/build.gradle

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
dependencies {
2+
implementation 'org.springframework.boot:spring-boot-starter-aop'
3+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
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+
}
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
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+
}

0 commit comments

Comments
 (0)