Spring 핵심원리 고급편 - 구체 클래스 기반 프록시 적용 2

목차

Spring 핵심원리 고급편 - 구체 클래스 기반 프록시 적용 2

public class OrderRepositoryConcreteProxy extends OrderRepositoryV2 {

private final OrderRepositoryV2 target;
private final LogTrace logTrace;

public OrderRepositoryConcreteProxy(OrderRepositoryV2 target, LogTrace logTrace) {
this.target = target;
this.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;
}
}
}

public class OrderServiceConcreteProxy extends OrderServiceV2 {

private final OrderServiceV2 target;
private final LogTrace logTrace;

public OrderServiceConcreteProxy(OrderServiceV2 target, LogTrace logTrace) {
super(null);
this.target = target;
this.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;
}
}
}
public class OrderControllerConcreteProxy extends OrderControllerV2 {

private final OrderControllerV2 target;
private final LogTrace logTrace;

public OrderControllerConcreteProxy(OrderControllerV2 target, LogTrace logTrace) {
super(null);
this.target = target;
this.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();
}
}
@Configuration
public class ConcreteProxyConfig {

@Bean
public OrderControllerV2 orderControllerV2(LogTrace logTrace){
OrderControllerV2 controllerImpl = new OrderControllerV2(orderServiceV2(logTrace));
return new OrderControllerConcreteProxy(controllerImpl, logTrace);
}

@Bean
public OrderServiceV2 orderServiceV2(LogTrace logTrace){
OrderServiceV2 serviceImpl = new OrderServiceV2(orderRepositoryV2(logTrace));
return new OrderServiceConcreteProxy(serviceImpl, logTrace);
}

@Bean
public OrderRepositoryV2 orderRepositoryV2(LogTrace logTrace){
OrderRepositoryV2 repositoryImpl = new OrderRepositoryV2();
return new OrderRepositoryConcreteProxy(repositoryImpl, logTrace);
}
}
//@Import(AppV1Config.class)
//@Import({AppV1Config.class, AppV2Config.class})
//@Import(InterfaceProxyConfig.class)
@Import(ConcreteProxyConfig.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 14:45:36.692  INFO 98295 --- [nio-8080-exec-1] h.p.trace.logtrace.ThreadLocalLogTrace   : [56d57615] OrderController.request()
2021-11-28 14:45:36.693 INFO 98295 --- [nio-8080-exec-1] h.p.trace.logtrace.ThreadLocalLogTrace : [56d57615] |-->OrderService.orderItem()
2021-11-28 14:45:36.693 INFO 98295 --- [nio-8080-exec-1] h.p.trace.logtrace.ThreadLocalLogTrace : [56d57615] | |-->OrderRepository.request()
2021-11-28 14:45:37.696 INFO 98295 --- [nio-8080-exec-1] h.p.trace.logtrace.ThreadLocalLogTrace : [56d57615] | |<--OrderRepository.request() time=1003ms
2021-11-28 14:45:37.696 INFO 98295 --- [nio-8080-exec-1] h.p.trace.logtrace.ThreadLocalLogTrace : [56d57615] |<--OrderService.orderItem() time=1003ms
2021-11-28 14:45:37.697 INFO 98295 --- [nio-8080-exec-1] h.p.trace.logtrace.ThreadLocalLogTrace : [56d57615] OrderController.request() time=1005ms
Share