문제점
JDK 동적 프록시
를 적용하고, 그렇지 않은 경우 CGLIB
를 적용하려면 어떻게 해야 하는가? → proxyFactoryJDK 동적 프록시
의 InvocationHandler와 CGLIB
MethodInterceptor 를 각각 중복으로 만들어 관리해야 하는가? → advice스프링에서 제공하는 동적 프록시를 통합해서 편리하게 만들어주는 프록시 팩토리(ProxyFactory
)기능을 사용하면 문제 해결 가능하다
Advisor = Advice + PointCut
MethodInterceptor - 스프링이 제공하는 코드
package org.aopalliance.intercept; // 패키지 주의, CGLIB와 다름
public interface MethodInterceptor extends Interceptor { // MethodInterceptor > Interceptor > Advice 상속 관계
Object invoke(MethodInvocation invocation) throws Throwable;
}
spring-aop
) 안에 들어있다TimeAdvice 생성
@Slf4j
public class TimeAdvice implements MethodInterceptor {
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
log.info("TimeProxy 실행");
long startTime = System.currentTimeMillis();
Object result = invocation.proceed(); // target 메서드 실행
long endTime = System.currentTimeMillis();
long resultTime = startTime - endTime;
log.info("result time 종료 = {}ms", resultTime);
return result;
}
}
프록시 팩토리 생성자에 target 정보를 파라미터로 전달하기 때문에 MethodInvocation invocation에 target 클래스 정보가 포함되어 있다고 생각하면 된다