🧩 Advice

정의

AdviceAOP에서 JoinPoint 전후에 실행되는 실제 부가 로직이다. 프록시가 대상 메서드를 가로챘을 때 실행되는 행동이라고 볼 수 있다.

로깅, 트랜잭션 처리, 예외 처리처럼 핵심 로직과 분리된 공통 기능을 담당한다.

🧩 역할

  • JoinPoint 전후에 부가 로직을 실행한다
  • 핵심 로직과 공통 기능을 분리한다
  • AOP의 실제 실행 내용을 정의한다
  • 로그, 보안, 트랜잭션, 예외 처리에 활용한다

🧩 종류

Advice 종류 설명
@Around 메서드 호출 전후를 모두 제어한다
@Before 메서드 실행 전에 동작한다
@AfterReturning 정상 종료 후 동작한다
@AfterThrowing 예외 발생 시 동작한다
@After 정상/예외와 상관없이 항상 동작한다

🛠 사용 예시

@Component
@Aspect
public class LoggingAspect {

    @Around("execution(* com.mvcvue.controller..*(..))")
    public Object aroundLogger(ProceedingJoinPoint joinPoint) throws Throwable {
        System.out.println("aroundLogger");
        return joinPoint.proceed();
    }

    @Before("execution(* com.mvcvue.controller..*(..))")
    public void beforeLogger(JoinPoint joinPoint) {
        System.out.println("beforeLogger");
    }

    @AfterReturning("execution(* com.mvcvue.controller..*(..))")
    public void afterReturningLogger(JoinPoint joinPoint) {
        System.out.println("afterReturningLogger");
    }

    @AfterThrowing("execution(* com.mvcvue.controller..*(..))")
    public void afterThrowingLogger(JoinPoint joinPoint) {
        System.out.println("afterThrowingLogger");
    }

    @After("execution(* com.mvcvue.controller..*(..))")
    public void afterLogger(JoinPoint joinPoint) {
        System.out.println("afterLogger");
    }
}

🧩 특징

  • Advice는 실제로 실행되는 부가 로직이다
  • 종류별로 실행 시점이 다르다
  • @Around는 가장 강력하게 제어할 수 있다
  • 동일한 JoinPoint에 여러 Advice가 적용될 수 있다

⚠️ 주의사항

  • 같은 JoinPoint에 여러 Advice가 걸리면 순서가 중요하다.
  • 순서가 필요하면 @Order를 함께 고려해야 한다.
  • @Aroundproceed() 호출을 빼먹으면 대상 메서드가 실행되지 않는다.
  • 예외 처리 로직은 @AfterThrowing과 함께 보는 것이 좋다.

📌 정리

  • Advice는 AOP에서 실제로 실행되는 부가 로직이다.
  • 실행 시점에 따라 @Before, @After, @Around 등으로 나뉜다.
  • 공통 기능을 핵심 로직과 분리할 때 유용하다.

연결문서

댓글남기기