Skip to content

Commit f156a30

Browse files
committed
고급편/섹션12: 예제 만들기 (#21)
섹션12: 스프링 AOP - 실전 예제: 5번 중 1번은 실패하는 상황 세팅
1 parent 8709751 commit f156a30

File tree

3 files changed

+58
-0
lines changed

3 files changed

+58
-0
lines changed
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package hello.aop.exam;
2+
3+
import org.springframework.stereotype.Repository;
4+
5+
@Repository
6+
public class ExamRepository {
7+
8+
private static int seq = 0;
9+
10+
/**
11+
* 5번에 1번 실패하는 요청
12+
* 실패할 때 AOP로 복구하도록 설정
13+
*/
14+
public String save(String itemId) {
15+
seq++;
16+
if (seq % 5 == 0) {
17+
throw new IllegalStateException("예외 발생");
18+
}
19+
return "ok";
20+
}
21+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package hello.aop.exam;
2+
3+
import lombok.RequiredArgsConstructor;
4+
import org.springframework.stereotype.Service;
5+
6+
@Service
7+
@RequiredArgsConstructor
8+
public class ExamService {
9+
10+
private final ExamRepository examRepository;
11+
12+
public void request(String itemId) {
13+
examRepository.save(itemId);
14+
}
15+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package hello.aop.exam;
2+
3+
import lombok.extern.slf4j.Slf4j;
4+
import org.junit.jupiter.api.Test;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.test.context.SpringBootTest;
7+
8+
@Slf4j
9+
@SpringBootTest
10+
public class ExamTest {
11+
12+
@Autowired
13+
ExamService examService;
14+
15+
@Test
16+
void test() {
17+
for (int i = 0; i < 5; i++) {
18+
log.info("client request i={}", i);
19+
examService.request("data" + i);
20+
}
21+
}
22+
}

0 commit comments

Comments
 (0)