Spring 핵심원리 고급편 - Proxy 패턴 인터페이스 없는 없는 구체 클래스

목차

Spring 핵심원리 고급편 - Proxy Pattern 인터페이스 없는 없는 구체 클래스

Repository

public class OrderRepositoryV2 {
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 OrderServiceV2 {

private final OrderRepositoryV2 orderRepository;

public OrderServiceV2(OrderRepositoryV2 orderRepository) {
this.orderRepository = orderRepository;
}

public void orderItem(String itemId) {
orderRepository.save(itemId);
}
}

Controller

@Slf4j
@RequestMapping
@ResponseBody
public class OrderControllerV2 {
private final OrderServiceV2 orderService;

public OrderControllerV2(OrderServiceV2 orderService) {
this.orderService = orderService;
}

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

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

Bean 등록

@Configuration
public class AppV2Config {

@Bean
public OrderControllerV2 orderControllerV2(){
return new OrderControllerV2(orderServiceV2());
}

@Bean
public OrderServiceV2 orderServiceV2(){
return new OrderServiceV2(orderRepositoryV2());
}

@Bean
public OrderRepositoryV2 orderRepositoryV2(){
return new OrderRepositoryV2();
}
}
//@Import(AppV1Config.class)
@Import({AppV1Config.class, AppV2Config.class})
@SpringBootApplication(scanBasePackages = "hello.proxy.app") //주의
public class ProxyApplication {

public static void main(String[] args) {
SpringApplication.run(ProxyApplication.class, args);
}

}
Share