Notice
Recent Posts
Recent Comments
Link
«   2024/06   »
1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 29
30
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

Pointcut - 스프링이 제공하는 pointcut 본문

인프런/[인프런] 스프링 핵심 원리 - 고급

Pointcut - 스프링이 제공하는 pointcut

Seung__ 2021. 12. 31. 16:21

스프링은 우리가 필요한 포인트컷을 이미 대부분 제공한다.

이번에는 스프링이 제공하는 NameMatchMethodPointcut 를 사용해서 구현해보자

 

 

1. NameMatchMethodPointcut


@Test
@DisplayName("스프링이 제공하는 포인트컷")
void advisorTest3(){
    ServiceInterface target = new ServiceImpl();
    ProxyFactory proxyFactory = new ProxyFactory(target);

    NameMatchMethodPointcut pointcut = new NameMatchMethodPointcut();
    pointcut.setMappedName("save"); //save인 경우에만 true/

    DefaultPointcutAdvisor advisor = new DefaultPointcutAdvisor(pointcut, new TimeAdvice());
    proxyFactory.addAdvisor(advisor);
    ServiceInterface proxy = (ServiceInterface) proxyFactory.getProxy();

    proxy.save();
    proxy.find();
}
  • NameMatchMethodPointcut 을 생성하고 setMappedNames(...) 으로 메서드 이름을 지정하면 포인트컷이 완성.
TimeProxy 실행
save 호출
TimeProxy 종료 resultTime = 0
find 호출
  • 실행 결과를 보면 save() 를 호출할 때는 어드바이스가 적용되지만,
    find() 를 호출할 때는 어드바이스가 적용되지 않는다.

 

2. 스프링이 제공하는 포인트 컷.


  • NameMatchMethodPointcut
    • 메서드 이름을 기반으로 매칭한다. 내부에서는 PatternMatchUtils 를 사용한다.
    • 예) *xxx* 허용
  • JdkRegexpMethodPointcut
    • JDK 정규 표현식을 기반으로 포인트컷을 매칭한다.
  • TruePointcut
    • 항상 참을 반환한다.
  • AnnotationMatchingPointcut
    • 애노테이션으로 매칭한다.
  • AspectJExpressionPointcut
    • aspectJ 표현식으로 매칭한다.
    • 실무에서 가장 많이 사용.
    • aspectJ표현식 기반

 

3. GitHub: 211231 Spring & Pointcut


 

GitHub - bsh6463/Spring_Advanced_2

Contribute to bsh6463/Spring_Advanced_2 development by creating an account on GitHub.

github.com

 

Comments