포인트컷(Pointcut) 은 어드바이스(Advice, 부가 기능)를 적용할 대상 메서드를 선정하는 표현식입니다.
execution(), within(), @annotation() 같은 표현식을 사용하여 대상 메서드를 필터링 execution())@Before("execution(* com.example.service.UserService.getUserById(..))")
public void beforeAdvice() {
System.out.println("getUserById() 메서드 실행 전에 수행됨");
}
설명:
execution(* com.example.service.UserService.getUserById(..))
→UserService클래스의getUserById메서드 실행 전에 AOP 적용
@Before("execution(* com.example.service..*(..))")
public void beforeAdvice() {
System.out.println("service 패키지 내의 메서드 실행 전에 수행됨");
}
설명:
com.example.service..*→service패키지 및 하위 패키지의 모든 메서드에 적용
@annotation())@Before("@annotation(com.example.annotation.LogExecutionTime)")
public void beforeAdvice() {
System.out.println("@LogExecutionTime 어노테이션이 붙은 메서드 실행 전에 수행됨");
}
설명:
@LogExecutionTime어노테이션이 붙은 메서드에만 AOP 적용
within())@Before("within(com.example.service.UserService)")
public void beforeAdvice() {
System.out.println("UserService의 모든 메서드 실행 전에 수행됨");
}
설명:
within(com.example.service.UserService)→UserService클래스 내 모든 메서드에 AOP 적용
| 표현식 | 설명 |
|---|---|
execution(* 패키지.클래스.메서드(..)) | 특정 메서드 실행 시 AOP 적용 |
execution(* 패키지..*(..)) | 특정 패키지 및 하위 패키지의 모든 메서드에 적용 |
@annotation(어노테이션) | 특정 어노테이션이 붙은 메서드에 적용 |
within(클래스명) | 특정 클래스 내 모든 메서드에 적용 |
args(타입) | 특정 타입의 매개변수를 가진 메서드에 적용 |
execution(), @annotation(), within() 등의 표현식을 사용하여 메서드를 선택