자동프록시 생성기(AnnotationAwareAspectJAutoProxyCreator)는 advisor를 자동으로 찾아와 필요한 곳에 프록시를 적용해준다.

이에 더해 자동프록시 생성기는 @Aspect 어노테이션을 찾아서 이것을 Advisor로 만들어준다.

아래와 같이 프록시를 적용하려는 로직에 @Aspect를 , ProceedingJoinPoint를 매개변수로 가지는 메서드를 만들고, @Auround 어노테이션을 붙여 적용범위를 지정하면, Advisor가 어플리케이션이 로드될 때 자동으로 등록된다.

 

@Slf4j
@Aspect
public class LogTraceAspect {
    private final LogTrace logTrace;

    public LogTraceAspect(LogTrace logTrace) {
        this.logTrace = logTrace;
    }
    @Around("execution(* hello.proxy.app..*(..))")
    public Object execute(ProceedingJoinPoint joinPoint) throws Throwable{
        TraceStatus status = null;
        try{
            String message = joinPoint.getSignature().toShortString();
            status = logTrace.begin(message);
            //로직 호출
            Object result = joinPoint.proceed();
            logTrace.end(status);
            return result;
        }catch (Exception e){
            logTrace.exception(status,e);
            throw e;
        }
    }
}

+ Recent posts