Notice
Recent Posts
Recent Comments
Link
«   2024/05   »
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 31
Archives
Today
Total
관리 메뉴

개발자되기 프로젝트

[빈 후처리기] 스프링이 제공하는 빈 후처리기2 본문

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

[빈 후처리기] 스프링이 제공하는 빈 후처리기2

Seung__ 2022. 1. 3. 22:27

1. Application 로딩 로그


EnableWebMvcConfiguration.requestMappingHandlerAdapter()
EnableWebMvcConfiguration.requestMappingHandlerAdapter() time=63ms
  • 애플리케이션 서버를 실행해보면, 스프링이 초기화 되면서 기대하지 않은 이러한 로그들이 올라온다. 
  • 그 이유는 지금 사용한 포인트컷이 단순히 메서드 이름에 "request*", "order*", "save*" 만 포함되어
    있으면 매칭 된다고 판단하기 때문이다.
  • 결국 스프링이 내부에서 사용하는 빈에도 메서드 이름에 request 라는 단어만 들어가 있으면 프록시가
    만들어지고 되고, 어드바이스도 적용되는 것이다.

  • 결론적으로 패키지에 메서드 이름까지 함께 지정할 수 있는 매우 정밀한 포인트컷이 필요하다.

 

2. AspectJExpressionPointcut


  • AspectJ라는 AOP에 특화된 포인트컷 표현식을 적용할 수 있다. 
  • AspectJ 포인트컷 표현식과 AOP는 조금 뒤에 자세히 설명하겠다. 
  • 지금은 특별한 표현식으로 복잡한 포인트컷을 만들 수 있구나 라고 대략 이해하면 된다??
@Configuration
@Import({AppV1Config.class, AppV2Config.class})
public class AutoProxyConfig {

    @Bean
    public Advisor advisor2(LogTrace logTrace){
        //point cut
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("execution(* hello.proxy.app..*(..))"); //여기 위치에 있어야 proxy적용 대상.

        //advice
        LogTraceAdvice advice = new LogTraceAdvice(logTrace);

        return new DefaultPointcutAdvisor(pointcut, advice);
    }
}

주의

  • advisor1 에 있는 @Bean 은 꼭 주석처리해주어야 한다. 
  • 그렇지 않으면 어드바이저가 중복 등록된다.
  • AspectJExpressionPointcut : AspectJ 포인트컷 표현식을 적용할 수 있다.
  • execution(* hello.proxy.app..*(..)) : AspectJ가 제공하는 포인트컷 표현식이다. 
  • * : 모든 반환 타입
  • hello.proxy.app.. : 해당 패키지와 그 하위 패키지
  • *(..) : * 모든 메서드 이름, (..) 파라미터는 상관 없음
  • 쉽게 이야기해서 hello.proxy.app 패키지와 그 하위 패키지의 모든 메서드는 포인트컷의 매칭 대상

 

  • 하지만 위와 같이 지정하면 모든 메서드가 대상이 된다. 따라서 로그가 없어야 하는 noLog()도 로그가 남는다..
  • advisor2 에서는 단순히 package 를 기준으로 포인트컷 매칭을 했기 때문이다.

 

3. noLog()를 빼보자


@Configuration
@Import({AppV1Config.class, AppV2Config.class})
public class AutoProxyConfig {

    @Bean
    public Advisor advisor3(LogTrace logTrace){
        //point cut
        AspectJExpressionPointcut pointcut = new AspectJExpressionPointcut();
        pointcut.setExpression("execution(* hello.proxy.app..*(..)) && !execution(* hello.proxy.app..noLog(..))"); //여기 위치에 있어야 proxy적용 대상.

        //advice
        LogTraceAdvice advice = new LogTraceAdvice(logTrace);

        return new DefaultPointcutAdvisor(pointcut, advice);
    }
}
  • noLog()를 대상에서 제외하기 위해 표현식을 다음과 같이 수정했다.
execution(* hello.proxy.app..*(..)) && !execution(* hello.proxy.app..noLog(..))
  • && : 두 조건을 모두 만족해야 함
  • ! : 반대

 

4. GitHub : 220103 AspectJExpressionPointcut


 

GitHub - bsh6463/Spring_Advanced_2

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

github.com

 

Comments