스프링 핵심 원리(고급편)

Ch11. 스프링 AOP(포인트컷) - @annotation, @args, bean

webmaster 2022. 4. 20. 15:12
728x90

@annotation

@annotation : 메서드가 주어진 애노테이션을 가지고 있는 조인 포인트를 매칭

  • @annotation(hello.aop.member.annotation.MethodAop)

Test

@Slf4j
@Import(AtAnnotationTest.AtAnnotationAspect.class)
@SpringBootTest
public class AtAnnotationTest {

    @Autowired
    MemberService memberService;

    @Test
    public void success() {
        log.info("memberService Proxy ={}", memberService.getClass());
        memberService.hello("helloA");
    }

    @Slf4j
    @Aspect
    static class AtAnnotationAspect {

        @Around("@annotation(hello.aop.member.annotation.MethodAop)")
        public Object doAtAnnotation(ProceedingJoinPoint joinPoint) throws
            Throwable {
            log.info("[@annotation] {}", joinPoint.getSignature());
            return joinPoint.proceed();
        }
    }

}

@args

@args : 전달된 실제 인수의 런타임 타입이 주어진 타입의 애노테이션을 갖는 조인 포인트

  • 전달된 인수의 런타임 타입에 @Check 애노테이션이 있는 경우에 매칭 한다.
@args(test.Check)

bean

bean : 스프링 전용 포인트컷 지시자, 빈의 이름으로 지정한다.

  • 스프링 빈의 이름으로 AOP 적용 여부를 지정한다. 이것은 스프링에서만 사용할 수 있는 특별한 지시자이다.
  • bean(orderService) || bean(*Repository)
  • * 과 같은 패턴을 사용할 수 있다.
@Slf4j
@Import(BeanTest.BeanAspect.class)
@SpringBootTest
public class BeanTest {

    @Autowired
    OrderService orderService;

    @Test
    public void success(){
        orderService.orderItem("itemA");
    }

    @Aspect
    static class BeanAspect{
        @Around("bean(orderService) || bean(*Repository)") //빈 이름이 바뀌지 않을때 사용하면 좋다
        public Object doLog(ProceedingJoinPoint joinPoint) throws Throwable {
            log.info("[bean] {}", joinPoint.getSignature());
            return joinPoint.proceed();
        }
    }
}
728x90