자동프록시 생성기(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;
}
}
}
'Java > 스프링 AOP' 카테고리의 다른 글
Spring AOP 용어정리 (0) | 2023.09.06 |
---|---|
Spring AOP - 핵심기능, 부가기능 , 애스펙트 (0) | 2023.09.05 |
실시간 하이픈 붙이기 (0) | 2023.08.30 |
빈 후처리기 (2) 스프링 AOP (0) | 2023.08.24 |
빈 후처리기 (1) - 빈 가로채기 (0) | 2023.08.22 |