Skip to content

Commit f8219b2

Browse files
committed
고급편/섹션12: 로그 출력 AOP (#21)
섹션12: 스프링 AOP - 실전 예제: 애노테이션을 활용한 AOP 예제
1 parent f156a30 commit f8219b2

File tree

5 files changed

+35
-0
lines changed

5 files changed

+35
-0
lines changed

스프링 로드맵5 고급편/aop/src/main/java/hello/aop/exam/ExamRepository.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package hello.aop.exam;
22

3+
import hello.aop.exam.annotation.Trace;
34
import org.springframework.stereotype.Repository;
45

56
@Repository
@@ -11,6 +12,7 @@ public class ExamRepository {
1112
* 5번에 1번 실패하는 요청
1213
* 실패할 때 AOP로 복구하도록 설정
1314
*/
15+
@Trace
1416
public String save(String itemId) {
1517
seq++;
1618
if (seq % 5 == 0) {

스프링 로드맵5 고급편/aop/src/main/java/hello/aop/exam/ExamService.java

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package hello.aop.exam;
22

3+
import hello.aop.exam.annotation.Trace;
34
import lombok.RequiredArgsConstructor;
45
import org.springframework.stereotype.Service;
56

@@ -9,6 +10,7 @@ public class ExamService {
910

1011
private final ExamRepository examRepository;
1112

13+
@Trace
1214
public void request(String itemId) {
1315
examRepository.save(itemId);
1416
}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package hello.aop.exam.annotation;
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 Trace {
11+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package hello.aop.exam.aop;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.aspectj.lang.JoinPoint;
5+
import org.aspectj.lang.annotation.Aspect;
6+
import org.aspectj.lang.annotation.Before;
7+
8+
@Slf4j
9+
@Aspect
10+
public class TraceAspect {
11+
12+
@Before("@annotation(hello.aop.exam.annotation.Trace)")
13+
public void doTrace(JoinPoint joinPoint) {
14+
Object[] args = joinPoint.getArgs(); // 넘어가는 인수 정보들
15+
log.info("[trace] {} args={}", joinPoint.getSignature(), args);
16+
}
17+
}

스프링 로드맵5 고급편/aop/src/test/java/hello/aop/exam/ExamTest.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,15 @@
11
package hello.aop.exam;
22

3+
import hello.aop.exam.aop.TraceAspect;
34
import lombok.extern.slf4j.Slf4j;
45
import org.junit.jupiter.api.Test;
56
import org.springframework.beans.factory.annotation.Autowired;
67
import org.springframework.boot.test.context.SpringBootTest;
8+
import org.springframework.context.annotation.Import;
79

810
@Slf4j
911
@SpringBootTest
12+
@Import(TraceAspect.class)
1013
public class ExamTest {
1114

1215
@Autowired

0 commit comments

Comments
 (0)