Pointcut
Join Point 중에서 Advice가 적용될 위치를 설정하는 기능
- AspectJ 표현식을 사용해서 지정
- 스프링의 AOP는 메서드 실행 지점만 Pointcut으로 설정 가능
포인트컷은 관심 Join Point를 결정하기 때문에 Advice가 실행되는 시기를 통제할 수 있다.
그리고 포인트컷을 편리하게 표현하기 위해 AspectJ 표현식을 사용한다.
@Pointcut("execution(* transfer(..))") // 포인트컷 표현식
private void anyOldTransfer() {} // 포인트컷 서명
Introduction to Pointcut Expressions in Spring | Baeldung
A quick and practical intro to Spring AOP and Pointcut Expressions.
www.baeldung.com
Pointcut 지시자
Pointcut 표현식은 execution 같은 포인트컷 지시자(pointcut Designator, PCD)로 시작한다.
Types of Pointcut designator | Description |
execution | 메서드 실행 Join Point를 매칭한다. 스프링 AOP에서 가장 많이 사용 but 기능도 복잡 |
within | 특정 타입 내의 Join Point를 매칭 |
args | 전달값 타입의 인스턴스인 Join Point |
this | 스프링 Bean 객체(스프링 AOP proxy)를 대상으로 하는 Join Point |
target | Target 객체(스프링 AOP proxy가 가르키는 실제 대상)를 대상으로 하는 Join Point |
@target | 실행 객체의 클래스에 주어진 타입의 Annotation이 있는 Join Point |
@within | 주어진 Annotation이 있는 타입 내 Join Point |
@annotation | 메서드가 주어진 Annotation을 가지고 있는 Join Point를 매칭 |
@args | 전달된 실제 파라미터의 런타임 타입이 주어진 타입의 Annotation을 갖는 Join Point |
bean | 스프링 전용 Pointcut designatior - Bean의 이름으로 Pointcut 지정 |
Pointcut 표현식 결합
표현식은 &&, ||, ! 를 사용하여 결합할 수 있다
그리고 이름으로 표현식을 참조할 수도 있다
@Pointcut("execution(public * *(..))")
private void anyPublicOperation() {} // (1)
@Pointcut("within(com.xyz.myapp.trading..*)")
private void inTrading() {} // (2)
@Pointcut("anyPublicOperation() && inTrading()")
private void tradingOperation() {} // (3)
- AnyPublicOperation은 메서드 실행 Join Point가 공용 메서드의 실행을 나타낼 때 일치한다
- inTrading은 메서드 실행이 거래 모듈에 있는 경우 일치한다
- tradingOperation은 메서드 실행이 거래 모듈의 공개 메서드를 나타낼 때 일치한다.
Pointcut 표현식 e.g.
5.4.3 - Examples
Core Technologies
In the preceding scenario, using @Autowired works well and provides the desired modularity, but determining exactly where the autowired bean definitions are declared is still somewhat ambiguous. For example, as a developer looking at ServiceConfig, how do
docs.spring.io
- 모든 공개 메서드 실행
execution(public * *(..))
- set 다음 이름으로 시작하는 모든 메서드 실행
execution(* set*(..))
- AccountService 인터페이스에 의해 정의된 모든 메서드의 실행
execution(* com.xyz.service.AccountService.*(..))
- service 패키지에 정의된 메서드 실행
execution(* com.xyz.service.*.*(..))
- 서비스 패키지 또는 해당 하위 패키지 중 하나에 정의된 메서드 실행
execution(* com.xyz.service..*.*(..))
- 서비스 패키지 내의 모든 조인 포인트
(Spring AOP에서만 메서드 실행)
within(com.xyz.service.*)
- 서비스 패키지 또는 하위 패키지 중 하나 내의 모든 조인 포인트
(Spring AOP에서만 메서드 실행)
within(com.xyz.service..*)
- AccountService 프록시가 인터페이스를 구현하는 모든 조인 포인트
(Spring AOP에서만 메서드 실행)
this(com.xyz.service.AccountService)
- AccountService 대상 객체가 인터페이스를 구현하는 모든 조인 포인트
(Spring AOP에서만 메서드 실행)
target(com.xyz.service.AccountService)
- 단일 매개변수를 사용하고 런타임에 전달된 인수가 Serializable과 같은 모든 조인 포인트
(Spring AOP에서만 메서드 실행)
args(java.io.Serializable)
- 대상 객체에 @Transactional 애너테이션이 있는 모든 조인 포인트
(Spring AOP에서만 메서드 실행)
@target(org.springframework.transaction.annotation.Transactional)
- 실행 메서드에 @Transactional 애너테이션이 있는 조인 포인트
(Spring AOP에서만 메서드 실행)
@annotation(org.springframework.transaction.annotation.Transactional)
- 단일 매개 변수를 사용하고 전달된 인수의 런타임 유형이
@Classified 애너테이션을 갖는 조인 포인트(Spring AOP에서만 메서드 실행)
@args(com.xyz.security.Classified)
- tradeService라는 이름을 가진 스프링 빈의 모든 조인 포인트
(Spring AOP에서만 메서드 실행)
bean(tradeService)
- 와일드 표현식 *Service라는 이름을 가진 스프링 빈의 모든 조인 포인트
bean(*Service)
'programming > SPRING' 카테고리의 다른 글
AOP - Annotation (AspectJ) (0) | 2022.06.20 |
---|---|
AOP - Join Point (0) | 2022.06.20 |
AOP - Advice (0) | 2022.06.20 |
[TBC]IoC 와 DI (0) | 2022.06.18 |
PSA (0) | 2022.06.15 |