목차
Spring 핵심원리 고급편 - 구체 클래스 기반 프록시
@Slf4j public class ConcreteLogic {
public String operation(){ log.info("ConcreteLogic 실행"); return "data"; } }
|
public class ConcreteClient {
private ConcreteLogic concreteLogic;
public ConcreteClient(ConcreteLogic concreteLogic) { this.concreteLogic = concreteLogic; }
public void execute(){ concreteLogic.operation(); } }
|
@Test void noProxy(){ ConcreteLogic concreteLogic = new ConcreteLogic(); ConcreteClient client = new ConcreteClient(concreteLogic); client.execute(); }
|
구체 클래스 기반 프록시 적용
자바에서 다형성은 인터페이스를 구현하든 상위 클래스를 상속하던 상위 타입만 맞으면 다형성이 적용 된다.
@Slf4j public class TimeProxy extends ConcreteLogic{
private ConcreteLogic concreteLogic;
public TimeProxy(ConcreteLogic concreteLogic) { this.concreteLogic = concreteLogic; }
@Override public String operation(){ log.info("TimeDecorator 실행"); long startTime = System.currentTimeMillis();
String result = concreteLogic.operation();
long endTime = System.currentTimeMillis(); long resultTime = endTime - startTime; log.info("TimeDecorator 종료 resultTime = {}ms", resultTime); return result; } }
|
@Test void addProxy(){ ConcreteLogic concreteLogic = new ConcreteLogic(); TimeProxy timeProxy = new TimeProxy(concreteLogic); ConcreteClient client = new ConcreteClient(timeProxy); client.execute(); }
|