Spring 핵심원리 고급편 - 인터페이스 프록시 1
목차 Spring 핵심원리 고급편 - CGLIB Spring 핵심원리 고급편 - Dynamic Proxy 2 Spring 핵심원리 고급편 - Dynamic Proxy 1 Spring 핵심원리 고급편 - 리플렉션 Spring 핵심원리 고급편 - 구체 클래스 기반 프록시 적용 2 Spring 핵심원리 고급편 - 구체 클래스 기반 프록시 Spring 핵심원리 고급편 - 인터페이스 프록시 1 Spring 핵심원리 고급편 - Decorator Pattern 2 Spring 핵심원리 고급편 - Decorator Pattern 1 Spring 핵심원리 고급편 - Proxy 패턴 컴포넌트 스캔으로 자동 빈 등록 Spring 핵심원리 고급편 - Proxy 패턴 인터페이스 없는 없는 구체 클래스 Spring 핵심원리 고급편 - Proxy 패턴 인터페이스와 구체 클래스 Spring 핵심원리 고급편 - Proxy 패턴 Spring 핵심원리 고급편 - Strategy 패턴 Spring 핵심원리 고급편 - Template Method 패턴 @RequiredArgsConstructorpublic class OrderRepositoryInterfaceProxy implements OrderRepositoryV1 { // 실제 객체 참조 private final OrderRepositoryV1 target; private final LogTrace logTrace; @Override public void save(String itemId) { TraceStatus status = null; try{ status = logTrace.begin("OrderRepository.request()"); // target 호출 target.save(itemId); logTrace.end(status); }catch (Exception ex){ logTrace.exception(status, ex); throw ex; } }} @RequiredArgsConstructorpublic class OrderServiceInterfaceProxy implements OrderServiceV1 { private final OrderServiceV1 target; private final LogTrace logTrace; @Override public void orderItem(String itemId) { TraceStatus status = null; try{ status = logTrace.begin("OrderService.orderItem()"); // target 호출 target.orderItem(itemId); logTrace.end(status); }catch (Exception ex){ logTrace.exception(status, ex); throw ex; } }} @RequiredArgsConstructorpublic class OrderControllerInterfaceProxy implements OrderControllerV1 { private final OrderControllerV1 target; private final LogTrace logTrace; @Override public String request(String itemId) { TraceStatus status = null; try{ status = logTrace.begin("OrderController.request()"); // target 호출 String result = target.request(itemId); logTrace.end(status); return result; }catch (Exception ex){ logTrace.exception(status, ex); throw ex; } } @Override public String noLog() { return target.noLog(); }} Proxy Bean 등록@Configurationpublic class InterfaceProxyConfig { @Bean public OrderControllerV1 orderController(LogTrace logTrace){ OrderControllerV1Impl controllerImpl = new OrderControllerV1Impl(orderService(logTrace)); return new OrderControllerInterfaceProxy(controllerImpl, logTrace); } @Bean public OrderServiceV1 orderService(LogTrace logTrace){ OrderServiceV1Impl orderServiceImpl = new OrderServiceV1Impl(orderRepository(logTrace)); return new OrderServiceInterfaceProxy(orderServiceImpl, logTrace); } @Bean public OrderRepositoryV1 orderRepository(LogTrace logTrace){ OrderRepositoryV1 orderRepositoryImpl = new OrderRepositoryV1Impl(); return new OrderRepositoryInterfaceProxy(orderRepositoryImpl, logTrace); }} //@Import(AppV1Config.class)//@Import({AppV1Config.class, AppV2Config.class})@Import(InterfaceProxyConfig.class)@SpringBootApplication(scanBasePackages = "hello.proxy.app") //주의public class ProxyApplication { public static void main(String[] args) { SpringApplication.run(ProxyApplication.class, args); } @Bean public LogTrace logTrace(){ return new ThreadLocalLogTrace(); }} 2021-11-28 13:54:09.793 INFO 97198 --- [nio-8080-exec-2] h.p.trace.logtrace.ThreadLocalLogTrace : [86b4baba] OrderController.request()2021-11-28 13:54:09.795 INFO 97198 --- [nio-8080-exec-2] h.p.trace.logtrace.ThreadLocalLogTrace : [86b4baba] |-->OrderService.orderItem()2021-11-28 13:54:09.795 INFO 97198 --- [nio-8080-exec-2] h.p.trace.logtrace.ThreadLocalLogTrace : [86b4baba] | |-->OrderRepository.request()2021-11-28 13:54:10.798 INFO 97198 --- [nio-8080-exec-2] h.p.trace.logtrace.ThreadLocalLogTrace : [86b4baba] | |<--OrderRepository.request() time=1003ms2021-11-28 13:54:10.798 INFO 97198 --- [nio-8080-exec-2] h.p.trace.logtrace.ThreadLocalLogTrace : [86b4baba] |<--OrderService.orderItem() time=1003ms2021-11-28 13:54:10.798 INFO 97198 --- [nio-8080-exec-2] h.p.trace.logtrace.ThreadLocalLogTrace : [86b4baba] OrderController.request() time=1005ms