execution | 메서드의 signature 기반으로 한 정교한 포인트 컷 작성법. 가장 많이 사용 | @Before(”execution(String *..TBean.getName())”) |
---|---|---|
within | 빈 클래스 기반으로 작성 | @Before(”within(com..TBeanImpl)”) |
bean | 빈의 이름 기반으로 작성 | @Before(”bean(pointcutTBean)”) |
execution에 작성하는 메서드의 패턴은 위 처럼 return_type, class, method_name, parameter로 나뉜다.
execution(public java.util.List<com.example.Myclass> *(..))
@Before("execution(* com..*(..)) || execution(* org..*(..))")
pointcut을 작성하면서 지시자로 execution 이외에도 within이나 bean, @annotation 등을 사용할 수도 있다.
within
실제 타겟 빈의 타입을 기준으로 해당 클래스, 인터페이스 내의 모든 메서드를 pointcut으로 지정할 수 있다.
빈에 적용된 애너테이션을 기준으로 pointcut을 지정할 때 사용된다.
@Before("within(com.practice.service.*)") // com.practice.service 소속 빈들의 모든 메서드
public void serviceBeans() {}
@Before("@within(org.springframework.stereotype.Service)") //@Service 타입 빈들의 모든 메서드
public void serviceBeans() {}
스프링의 빈이름을 이용하여 포인트 컷을 지정할 때 사용된다.
빈의 구체적인 타입이나 클래스의 구조와 무관하게 빈의 이름만으로 pointcut을 지정한다.
@Before("bean(*Service)")
public void serviceBeans() {}
지정하는 특정 애너테이션이 선언된 메서드에 대해서 advice를 지정할 때 사용
@Before("@annotation(org.springframework.transaction.annotation.Transactional)")
public void transactionalMethods() {}
package com.practice.aop;
public class Pointcuts {
@Pointcut("execution(* com.practice.service.*.*(..))")
public void serviceLayerExecution() {
}
}
package com.practice.aop;
@Aspect
@Component
public class LoggingAspect {
@Before("com.practice.aop.Pointcuts.serviceLayerExecution()")
public void logBefore(JoinPoint jp) {
}
}