Spring 핵심원리 고급편 - 인터페이스 프록시 1

목차

@RequiredArgsConstructor
public 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;
}
}
}
@RequiredArgsConstructor
public 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;
}
}
}
@RequiredArgsConstructor
public 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 등록


@Configuration
public 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=1003ms
2021-11-28 13:54:10.798 INFO 97198 --- [nio-8080-exec-2] h.p.trace.logtrace.ThreadLocalLogTrace : [86b4baba] |<--OrderService.orderItem() time=1003ms
2021-11-28 13:54:10.798 INFO 97198 --- [nio-8080-exec-2] h.p.trace.logtrace.ThreadLocalLogTrace : [86b4baba] OrderController.request() time=1005ms
Share