AOP란?

Aspect Oriented Programming - 관점 지향 프로그래밍

모듈이 실행할 로직을 핵심 관점 및 부가 관점으로 나누어서 모듈화의 기준으로 삼는 것

핵심 관심사 예시: 비즈니스 로직

부가 관심사 예시: 예외 처리 / 로깅 / 파일 입출력

이러한 부가 관심사는 여러 코드에서 반복적으로 사용되지만 비즈니스 로직과는 구분되므로 이를 별도로 모듈화하여 핵심 로직과 분리된 구조를 만드는 것이 AOP의 취지

Spring AOP

의존성에 spring-boot-starter-aop 추가

하지만 다른 패키지들에 기본적으로 포함되어 있음

implementation 'org.springframework.boot:spring-boot-starter-aop'

// 이런 패키지에 이미 포함돼 있음
implementation 'org.springframework.boot:spring-boot-starter-security'
implementation 'org.springframework.boot:spring-boot-starter-web'

(스프링 부트에선 불필요)

스프링 어플리케이션 진입점에 @EnableAspectJAutoProxy 추가하여 AOP 활성화

@Aspect 사용하여 Aspect 클래스 선언 후 @Component로 스프링 컨테이너에 등록

@Aspect
@Component
@RequiredArgsConstructor
public class CooldownAspect {
		// 로직 작성...
}

이후 해당 Aspect에서 실행할 메서드 작성

@Before("@annotation(annotation)") // 적용 대상 & 실행 시점
public void checkCooldown(JoinPoint joinPoint, Cooldown annotation){ // 파라미터 구성은 자유
		// 로직 작성...
}