Skip to content

Commit

Permalink
고급편/섹션12: 로그 출력 AOP (#21)
Browse files Browse the repository at this point in the history
섹션12: 스프링 AOP - 실전 예제: 애노테이션을 활용한 AOP 예제
  • Loading branch information
toychip committed Apr 1, 2024
1 parent f156a30 commit f8219b2
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hello.aop.exam;

import hello.aop.exam.annotation.Trace;
import org.springframework.stereotype.Repository;

@Repository
Expand All @@ -11,6 +12,7 @@ public class ExamRepository {
* 5번에 1번 실패하는 요청
* 실패할 때 AOP로 복구하도록 설정
*/
@Trace
public String save(String itemId) {
seq++;
if (seq % 5 == 0) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package hello.aop.exam;

import hello.aop.exam.annotation.Trace;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

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

private final ExamRepository examRepository;

@Trace
public void request(String itemId) {
examRepository.save(itemId);
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package hello.aop.exam.annotation;

import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;

@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
public @interface Trace {
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
package hello.aop.exam.aop;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;

@Slf4j
@Aspect
public class TraceAspect {

@Before("@annotation(hello.aop.exam.annotation.Trace)")
public void doTrace(JoinPoint joinPoint) {
Object[] args = joinPoint.getArgs(); // 넘어가는 인수 정보들
log.info("[trace] {} args={}", joinPoint.getSignature(), args);
}
}
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package hello.aop.exam;

import hello.aop.exam.aop.TraceAspect;
import lombok.extern.slf4j.Slf4j;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Import;

@Slf4j
@SpringBootTest
@Import(TraceAspect.class)
public class ExamTest {

@Autowired
Expand Down

0 comments on commit f8219b2

Please sign in to comment.