Skip to content

Commit

Permalink
고급편/섹션12: 예제 만들기 (#21)
Browse files Browse the repository at this point in the history
섹션12: 스프링 AOP - 실전 예제: 5번 중 1번은 실패하는 상황 세팅
  • Loading branch information
toychip committed Apr 1, 2024
1 parent 8709751 commit f156a30
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package hello.aop.exam;

import org.springframework.stereotype.Repository;

@Repository
public class ExamRepository {

private static int seq = 0;

/**
* 5번에 1번 실패하는 요청
* 실패할 때 AOP로 복구하도록 설정
*/
public String save(String itemId) {
seq++;
if (seq % 5 == 0) {
throw new IllegalStateException("예외 발생");
}
return "ok";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package hello.aop.exam;

import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

@Service
@RequiredArgsConstructor
public class ExamService {

private final ExamRepository examRepository;

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

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;

@Slf4j
@SpringBootTest
public class ExamTest {

@Autowired
ExamService examService;

@Test
void test() {
for (int i = 0; i < 5; i++) {
log.info("client request i={}", i);
examService.request("data" + i);
}
}
}

0 comments on commit f156a30

Please sign in to comment.