Reflection

핸들러 메서드 호출 원리 이해

사전 지식 : 리플렉션

  • 스프링의 핸들러 메서드를 호출하기 위해 내부적으로 사용되는 클래스는 InvocableHandlerMethod와 이를 확장한 ServletInvocableHandlerMethod 클래스가 있다.

  • 메서드 호출 시 메서드 파라미터를 해석하기 위한 HandlerMethodArgumentResolver와 메서드 반환 값을 처리하기 위한 HandlerMethodReturnValueHandler와 같은 클래스와 연계하여 요청을 처리한다.

  • InvocableHandlerMethod는 범용적 메서드 호출 클래스이며, ServletInvocableHandlerMethod HTTP 요청 및 응답 처리 클래스로 구분할 수 있다.

img.png

InvocableHandlerMethod

  • HTTP 프로토콜에 독립적이며 범용적인 메서드 호출 기능을 제공하고 매개변수 처리를 위해 HandlerMethodArgumentResolver를 사용한다.

  • 메서드를 호출한 결과를 반환하지만 내부적으로 반환값을 추가로 처리하지는 않는다.

img_2.png

InvocableHandlerMethod 사용 예제

ServletInvocableHandlerMethod

  • InvocableHandlerMethod 클래스를 상속한 클래스로 Servlet 환경(HttpServletRequest / HttpServletResponse)에 특화되어 있으며 HTTP 요청 및 응답을 포함한 핸들러 메서드를 호출한다.

  • 메서드의 반환 값을 HandlerMethodReturnValueHandler를 통해 처리할 수 있으며 메서드 레벨의 @ResponseStatus 어노테이션을 지원하여 HTTP 응답 상태를 설정할 수 있다.

img_1.png

ServletInvocableHandlerMethod 사용 예제


메서드 파라미터 실행 구조 이해

핵심 클래스

  • ServletInvocableHandlerMethod

    • HTTP 요청 데이터를 받아 메서드를 실행시키는 클래스

    • HandlerMethodArgumentResolver를 통해 요청 데이터를 메서드의 매개변수에 맞게 변환하고 이를 사용해 메서드를 호출한다.

  • AnnotatedMethod

    • 호출 메서드 및 메서드 파라미터 정보를 가지고 있으며 파라미터에 어노테이션 정보를 탐색할 수 있다.

  • MethodParameter

    • 메서드에 있는 파라미터의 정보를 관리하는 클래스

    • 메서드의 어노테이션을 분석하고 파라미터가 어디에 있고 어떤 타입인지를 편리하게 알 수 있게 해준다.

  • MethodIntrospector

    • 메서드를 탐색할 수 있게 해주는 클래스

    • 어노테이션이 적용된 메서드들의 리스트를 구하거나 메서드의 메타데이터를 찾도록 도와준다.

img_3.png

MethodIntrospector

AnnotatedMethod & MethodParameter

스캔한 메서드들의 파라미터 메타 정보들을 가지는 MethodParameter 객체를 생성한다.

method1의 파라미터 메타 정보 img_4.png

method2의 파라미터 메타 정보 img_5.png

ServletInvocableHandlerMethod

  • InvocableHandlerMethod 클래스를 확장한 웹 처리용 클래스로써 핸들러의 메서드를 호출하는 기능을 가지고 있다.

  • HTTP 요청 정보를 분석하고 해당 요청에 매핑되어 있는 AnnotatedMethod 객체를 찾아 새로운 ServletInvocableHandlerMethod를 생성하고 전달한다.

  • AnnotatedMethod에 저장되어 있는 Method, MethodParameter 정보들을 ServletInvocableHandlerMethod에 복사한다.

img_6.png

RequestMappingHandlerAdapter에서 HTTP 요청 마다 새로운 ServletInvocableHandlerMethod를 생성한다.

img_7.png
img_8.png
  • super() 생성자를 반복해 최상위 클래스인 AnnotatedMethod 클래스까지 가서 초기화 시점에 초기화된 메서드와 파라미터 정보들을 그대로 복사한다.

  • 이후 요청 처리에서는 이 복사된 값들을 그대로 사용한다.

img_9.png

메서드 파라미터 실행 흐름도 요약

img_10.png

Last updated