Spring AOP 어노테이션 정리

백엔드&인프라 추종자·2025년 2월 27일

스프링 공부

목록 보기
18/35

Spring AOP에서는 AspectJ 스타일의 어노테이션을 사용하여 AOP 기능을 간편하게 적용할 수 있습니다.
다음은 주요 어노테이션과 그 역할에 대한 설명입니다.


Spring AOP 어노테이션 정리

1️⃣ @Aspect - 애스펙트 정의

  • 클래스가 AOP 기능(횡단 관심사) 을 포함하는 Aspect(애스펙트) 임을 선언합니다.
  • 이 클래스는 공통 기능(로깅, 트랜잭션, 보안 검사 등)을 정의하는 역할을 합니다.
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {
    // Advice(공통 기능)를 여기에 추가
}

2️⃣ @Pointcut - AOP 적용 대상 지정

  • 조인 포인트(Join Point) 중에서 특정 메서드만 선택할 때 사용합니다.
  • 여러 Advice에서 재사용할 수 있습니다.
import org.aspectj.lang.annotation.Pointcut;

@Pointcut("execution(* com.example.service.*.*(..))") 
public void serviceMethods() {} // service 패키지의 모든 메서드가 대상

🔹 execution() 표현식

@Pointcut("execution(반환타입 패키지명.클래스명.메서드명(매개변수))")
표현식 예제의미
execution(* com.example..*(..))com.example 패키지 이하 모든 메서드 적용
execution(* com.example.service.UserService.getUser(..))UserService.getUser() 메서드만 적용
execution(public * *(..))모든 public 메서드 적용

3️⃣ @Before - 메서드 실행 이전에 실행

  • 조인 포인트(메서드)가 실행되기 전에 실행됩니다.
  • 주로 로그 기록, 보안 검사, 유효성 검사 등에 사용됩니다.
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Aspect;
import org.springframework.stereotype.Component;

@Aspect
@Component
public class LoggingAspect {

    @Before("execution(* com.example.service.*.*(..))") 
    public void beforeAdvice() {
        System.out.println("[로그] 메서드 실행 전");
    }
}

4️⃣ @After - 메서드 실행 에 실행

  • 조인 포인트(메서드)가 실행된 실행됩니다.
  • 메서드가 정상 종료되거나 예외가 발생해도 실행됩니다.
import org.aspectj.lang.annotation.After;

@Aspect
@Component
public class LoggingAspect {

    @After("execution(* com.example.service.*.*(..))") 
    public void afterAdvice() {
        System.out.println("[로그] 메서드 실행 후");
    }
}

5️⃣ @AfterReturning - 메서드가 정상적으로 실행된 후 실행

  • 예외 없이 정상적으로 실행된 경우에만 실행됩니다.
  • 반환 값을 확인하거나 변경하는 용도로 사용할 수 있습니다.
import org.aspectj.lang.annotation.AfterReturning;

@Aspect
@Component
public class LoggingAspect {

    @AfterReturning(pointcut = "execution(* com.example.service.*.*(..))", returning = "result")
    public void afterReturningAdvice(Object result) {
        System.out.println("[로그] 정상 실행 완료. 반환 값: " + result);
    }
}

6️⃣ @AfterThrowing - 메서드 실행 중 예외가 발생했을 때 실행

  • 메서드 실행 중 예외가 발생하면 실행됩니다.
  • 예외 정보를 받아서 로깅하거나 특정 처리를 수행할 수 있습니다.
import org.aspectj.lang.annotation.AfterThrowing;

@Aspect
@Component
public class LoggingAspect {

    @AfterThrowing(pointcut = "execution(* com.example.service.*.*(..))", throwing = "ex")
    public void afterThrowingAdvice(Exception ex) {
        System.out.println("[예외 발생] " + ex.getMessage());
    }
}

7️⃣ @Around - 메서드 실행 전후로 감싸기

  • 메서드 실행 전, 후, 예외 발생 시 모두 제어 가능
  • 실행 시간을 측정하거나, 실행 여부를 결정하는 용도로 사용됩니다.
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.ProceedingJoinPoint;

@Aspect
@Component
public class LoggingAspect {

    @Around("execution(* com.example.service.*.*(..))")
    public Object aroundAdvice(ProceedingJoinPoint joinPoint) throws Throwable {
        long start = System.currentTimeMillis();
        
        System.out.println("[로그] 메서드 실행 시작");
        Object result = joinPoint.proceed(); // 실제 메서드 실행
        System.out.println("[로그] 메서드 실행 완료");

        long executionTime = System.currentTimeMillis() - start;
        System.out.println("[로그] 실행 시간: " + executionTime + "ms");

        return result;
    }
}

Spring AOP 어노테이션 요약

어노테이션설명실행 시점
@Aspect클래스가 AOP 기능을 포함하는 Aspect 임을 선언-
@Pointcut특정 메서드를 AOP 적용 대상으로 설정-
@Before메서드 실행 전에 실행메서드 실행 전
@After메서드 실행 후 (예외 발생 여부 관계없음)메서드 실행 후
@AfterReturning메서드가 정상 실행된 후 실행정상 실행 후
@AfterThrowing메서드 실행 중 예외 발생 시 실행예외 발생 시
@Around메서드 실행 전후를 감싸서 실행전, 후, 예외 발생 시

결론

  • @Before → 메서드 실행
  • @After → 메서드 실행 후 (예외 여부 상관없음)
  • @AfterReturning → 메서드 정상 종료 후
  • @AfterThrowing → 메서드 실행 중 예외 발생 시
  • @Around → 메서드 실행 전, 후, 예외 처리까지 모두 가능

Spring AOP의 어노테이션을 적절히 활용하면 비즈니스 로직과 공통 관심사를 분리할 수 있어 코드 유지보수가 쉬워집니다. 🚀

profile
AI 답변 글을 주로 올립니다.

0개의 댓글