AOP 란
횡단 관심사, Cross-cutting concerns 를 분리하여 모듈성을 높히는 프로그래밍 패러다임이다.

로깅, 보안처리, 트랜잭션 처리에 관한 로직은 프로젝트의 많은 모듈들이 관심을 가질만한 공통 관심사이다. 이런 로직들은 보통 모듈의 핵심 기능을 가로질러 등장하며 클래스의 응집도를 낮춘다. AOP 는 이런 횡단 관심사를 핵심 로직과 분리하여 모듈성과 사용성을 높히는 프로그래밍 패러다임이다.
Spring 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
- 어드바이저 생성 기능

Spring AOP 프록시 적용 사례
관련 annotation | 횡단 관심사 |
@EnableGlobalMethodSecurity , @Secured | 보안 처리 |
@Transactional | 트랜잭션 처리 |
@Repository | 예외 변환 기능 |