횡단 관심사, Cross-cutting concerns 를 분리하여 모듈성을 높히는 프로그래밍 패러다임이다.
로깅, 보안처리, 트랜잭션 처리에 관한 로직은 프로젝트의 많은 모듈들이 관심을 가질만한 공통 관심사이다. 이런 로직들은 보통 모듈의 핵심 기능을 가로질러 등장하며 클래스의 응집도를 낮춘다. AOP 는 이런 횡단 관심사를 핵심 로직과 분리하여 모듈성과 사용성을 높히는 프로그래밍 패러다임이다.
스프링 AOP를 적용하는 예제를 살펴보고 간단히 관련 클래스와 용어를 정리해보자.
@Aspect
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;
}
}
}
Pointcut
AspectJExpressionPointCut
Advice
Advisor
Pointcut
+ Advice
@Aspect
- 어드바이저 생성 기능