Spring 핵심원리 고급편 - Proxy 패턴 컴포넌트 스캔으로 자동 빈 등록

목차

Spring 핵심원리 고급편 - Proxy Pattern 컴포넌트 스캔으로 자동 빈 등록

@Repository
public class OrderRepositoryV3 {
public void save(String itemId) {
if(itemId.equals("ex")){
throw new IllegalStateException("예외 발생!");
}
sleep(1000);
}

private void sleep(int millis) {
try {
Thread.sleep(millis);
}catch (InterruptedException ex){
ex.printStackTrace();
}
}
}
@Service
public class OrderServiceV3 {
private final OrderRepositoryV3 orderRepository;

public OrderServiceV3(OrderRepositoryV3 orderRepository) {
this.orderRepository = orderRepository;
}

public void orderItem(String itemId) {
orderRepository.save(itemId);
}
}
@RestController
@Slf4j
public class OrderControllerV3 {
private final OrderServiceV3 orderService;

public OrderControllerV3(OrderServiceV3 orderService) {
this.orderService = orderService;
}

@GetMapping("/v3/request")
public String request(String itemId) {
orderService.orderItem(itemId);
return "ok";
}

@GetMapping("/v3/no-log")
public String noLog() {
return "ok";
}
}
Share