AOP(Aspect-Oriented Programming, 관점 지향 프로그래밍)는 객체 지향 프로그래밍(OOP)으로 해결하기 어려운 공통 기능(횡단 관심사, Cross-Cutting Concern) 을 효과적으로 분리하여 모듈화하는 프로그래밍 패러다임입니다.
횡단 관심사(Cross-Cutting Concern)
애스펙트(Aspect)
LoggingAspect로 따로 정의조인 포인트(Join Point)
포인트컷(Pointcut)
어드바이스(Advice)
위빙(Weaving)
Spring에서는 AOP를 지원하며, @Aspect와 @Around 같은 어노테이션을 활용하여 쉽게 구현할 수 있습니다.
import org.aspectj.lang.annotation.*;
import org.aspectj.lang.ProceedingJoinPoint;
import org.springframework.stereotype.Component;
@Aspect
@Component
public class LoggingAspect {
@Around("execution(* com.example.service.*.*(..))")
public Object logExecutionTime(ProceedingJoinPoint joinPoint) throws Throwable {
long start = System.currentTimeMillis();
Object result = joinPoint.proceed();
long executionTime = System.currentTimeMillis() - start;
System.out.println(joinPoint.getSignature() + " executed in " + executionTime + "ms");
return result;
}
}
Spring AOP를 활용하면 비즈니스 로직과 부가 기능을 명확하게 분리할 수 있어 유지보수성이 향상됩니다.