Skip to content

Commit

Permalink
고급편/섹션4: 구체 클래스 기반 프록시 - 적용 (#5)
Browse files Browse the repository at this point in the history
섹션4: 프록시 패턴과 데코레이터 패턴
  • Loading branch information
toychip committed Mar 6, 2024
1 parent bdda5f7 commit ea2c9c2
Show file tree
Hide file tree
Showing 5 changed files with 133 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import hello.proxy.config.AppV1Config;
import hello.proxy.config.AppV2Config;
import hello.proxy.config.v1_proxy.concrete_proxy.ConcreteProxyConfig;
import hello.proxy.config.v1_proxy.interface_proxy.InterfaceProxyConfig;
import hello.proxy.trace.logtrace.LogTrace;
import hello.proxy.trace.logtrace.ThreadLocalLogTrace;
Expand All @@ -12,7 +13,8 @@

//@Import(AppV1Config.class)
//@Import({AppV1Config.class, AppV2Config.class})
@Import(InterfaceProxyConfig.class)
//@Import(InterfaceProxyConfig.class)
@Import(ConcreteProxyConfig.class)
@SpringBootApplication(scanBasePackages = "hello.proxy.app")
// 주의 config 하위 클래스 AppV1Config 등을 컴포넌트 스캔 대상에 두지 않기 위해 작업
public class ProxyApplication {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package hello.proxy.config.v1_proxy.concrete_proxy;

import hello.proxy.app.v2.OrderControllerV2;
import hello.proxy.app.v2.OrderRepositoryV2;
import hello.proxy.app.v2.OrderServiceV2;
import hello.proxy.trace.logtrace.LogTrace;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class ConcreteProxyConfig {

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

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

@Bean
public OrderRepositoryV2 orderRepositoryV2(LogTrace logTrace) {
OrderRepositoryV2 repositoryImpl = new OrderRepositoryV2();
return new OrderRepositoryConcreteProxy(repositoryImpl, logTrace);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package hello.proxy.config.v1_proxy.concrete_proxy;

import hello.proxy.app.v2.OrderControllerV2;
import hello.proxy.trace.TraceStatus;
import hello.proxy.trace.logtrace.LogTrace;

public class OrderControllerConcreteProxy extends OrderControllerV2 {

private final OrderControllerV2 target;
private final LogTrace logTrace;

public OrderControllerConcreteProxy(final OrderControllerV2 target, final LogTrace logTrace) {
super(null);
this.target = target;
this.logTrace = logTrace;
}

@Override
public String request(final String itemId) {
TraceStatus status = null;
try {
status = logTrace.begin("OrderController.request()");
// target 호출
String result = target.request("itemId");
logTrace.end(status);

return result;
} catch (Exception e) {
logTrace.exception(status, e);
throw e;
}
}

@Override
public String noLog() {
return target.noLog();
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package hello.proxy.config.v1_proxy.concrete_proxy;

import hello.proxy.app.v2.OrderRepositoryV2;
import hello.proxy.trace.TraceStatus;
import hello.proxy.trace.logtrace.LogTrace;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

@Slf4j
@RequiredArgsConstructor
public class OrderRepositoryConcreteProxy extends OrderRepositoryV2 {

private final OrderRepositoryV2 target;
private final LogTrace logTrace;
@Override
public void save(final String itemId) {
TraceStatus status = null;
try {
status = logTrace.begin("OrderRepository.request()");

// target 호출
target.save("itemId");

logTrace.end(status);
} catch (Exception e) {
logTrace.exception(status, e);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package hello.proxy.config.v1_proxy.concrete_proxy;

import hello.proxy.app.v2.OrderServiceV2;
import hello.proxy.trace.TraceStatus;
import hello.proxy.trace.logtrace.LogTrace;

public class OrderServiceConcreteProxy extends OrderServiceV2 {

private final OrderServiceV2 target;
private final LogTrace logTrace;


public OrderServiceConcreteProxy(final OrderServiceV2 target, final LogTrace logTrace) {
super(null);
this.target = target;
this.logTrace = logTrace;
}

@Override
public void orderItem(final String itemId) {
TraceStatus status = null;
try {
status = logTrace.begin("OrderService.orderItem()");

// target 호출
target.orderItem("itemId");

logTrace.end(status);
} catch (Exception e) {
logTrace.exception(status, e);
}
}
}

0 comments on commit ea2c9c2

Please sign in to comment.