@AfterThrowing : 이 조인 포인트는 메소드가 예외를 던진 시점에서 실행된다. 즉, 타겟에서 예외가 발생하면 그 즉시 @AfterThrowing이 가리키는 어드바이스가 실행된다. 

 

@AfterReturn과 같이 어노테이션 속성에 pointcut은 물론 throwing이 존재한다. 형식은 @AfterReturning과 같이 해야한다.

@Aspect
@Order(1)
@Component
public class AopAfterThrowing {
    @Pointcut("execution(public * com.rosoa0475.studyserver..*(..))")
    private void declarePointcut(){};

    @AfterThrowing(
            pointcut = "declarePointcut()",
            throwing = "exception")
    public void exceptionLog(JoinPoint joinPoint, Exception exception) {
        System.out.println(joinPoint.getSignature().toShortString());
        System.out.println("Exception caught: " + exception.getMessage());
    }
}

 

@AfterThrowing은 예외 객체만 가져오는 것이고 예외 전파가 안 된 것이므로 예외를 처리할 수 없다. 예외 처리까지 하려면 이후에 배울 @Around를 사용해야 한다. 결론적으로 이 JoinPoint를 통해서는 어떤 메소드에서 예외가 발생했는지 알 수 있다는 장점이 있다.

 

@After :

  • 이 조인 포인트는 메소드가 성공하든 실패(예외발생)하든 무조건 어드바이스를 실행하는 조인 포인트이다.
  • 리턴 객체나 예외 객체를 받는 부분이 없다. 즉, returning, throwing 속성이 없다.
  • 성공 또는 예외의 경우에도 실행될 수 있어야 한다. 코드가 메소드의 성공이나 예외에 의존하지 않아야 한다.
...

@After("declarePointcut()")
public void afterLog(JoinPoint joinPoint){
    System.out.println(joinPoint.getSignature().toShortString());
}

 

'Spring Boot > AOP' 카테고리의 다른 글

Spring Boot AOP @Around  (0) 2024.07.31
Spring Boot AOP 실습3 JoinPoint, @AfterReturning  (0) 2024.07.30
Spring Boot AOP 실습2 - @Order  (0) 2024.07.30
Spring Boot AOP 실습1 - @Before, Pointcut  (0) 2024.07.30
Spring Boot AOP 개념  (0) 2024.07.30

+ Recent posts