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

tokkaiiii·2025년 5월 17일

spring-mvc

목록 보기
22/27

AnnotatedMethod 클래스

 public AnnotatedMethod(Method method) {
    Assert.notNull(method, "Method is required");
    this.method = method;
    this.bridgedMethod = BridgeMethodResolver.findBridgedMethod(method);
    ReflectionUtils.makeAccessible(this.bridgedMethod);
    this.parameters = this.initMethodParameters();
  }
 private MethodParameter[] initMethodParameters() {
    int count = this.bridgedMethod.getParameterCount();
    MethodParameter[] result = new MethodParameter[count];

    for(int i = 0; i < count; ++i) {
      result[i] = new AnnotatedMethodParameter(i);
    }

    return result;
  }

이렇게 지나가고
AbstractHandlerMethodMapping 클래스인데
구체적 클래스가 RequestMappingHandlerMapping으로 나온다

 public void afterPropertiesSet() {
    this.initHandlerMethods();
  }

RequestMapping 어노테이션이 선언된 메소드를 가져옴

protected void detectHandlerMethods(Object handler) {
    Class var10000;
    if (handler instanceof String beanName) {
      var10000 = this.obtainApplicationContext().getType(beanName);
    } else {
      var10000 = handler.getClass();
    }

    Class<?> handlerType = var10000;
    if (handlerType != null) {
      Class<?> userType = ClassUtils.getUserClass(handlerType);
      Map<Method, T> methods = MethodIntrospector.selectMethods(userType, (method) -> {
        try {
          return this.getMappingForMethod(method, userType);
        } catch (Throwable ex) {
          throw new IllegalStateException("Invalid mapping on handler class [" + userType.getName() + "]: " + method, ex);
        }
      });
      if (this.logger.isTraceEnabled()) {
        this.logger.trace(this.formatMappings(userType, methods));
      } else if (this.mappingsLogger.isDebugEnabled()) {
        this.mappingsLogger.debug(this.formatMappings(userType, methods));
      }

      methods.forEach((method, mapping) -> {
        Method invocableMethod = AopUtils.selectInvocableMethod(method, userType);
        this.registerHandlerMethod(handler, invocableMethod, mapping);
      });
    }

  }

메소드를 저장한다

this.registerHandlerMethod(handler, invocableMethod, mapping);
  public void register(T mapping, Object handler, Method method) {
      this.readWriteLock.writeLock().lock();

      try {
        HandlerMethod handlerMethod = AbstractHandlerMethodMapping.this.createHandlerMethod(handler, method);
        this.validateMethodMapping(handlerMethod, mapping);
        handlerMethod = handlerMethod.createWithValidateFlags();
        Set<String> directPaths = AbstractHandlerMethodMapping.this.getDirectPaths(mapping);

        for(String path : directPaths) {
          this.pathLookup.add(path, mapping);
        }

        String name = null;
        if (AbstractHandlerMethodMapping.this.getNamingStrategy() != null) {
          name = AbstractHandlerMethodMapping.this.getNamingStrategy().getName(handlerMethod, mapping);
          this.addMappingName(name, handlerMethod);
        }

        CorsConfiguration corsConfig = AbstractHandlerMethodMapping.this.initCorsConfiguration(handler, method, mapping);
        if (corsConfig != null) {
          corsConfig.validateAllowCredentials();
          corsConfig.validateAllowPrivateNetwork();
          this.corsLookup.put(handlerMethod, corsConfig);
        }

        this.registry.put(mapping, new MappingRegistration(mapping, handlerMethod, directPaths, name, corsConfig != null));
      } finally {
        this.readWriteLock.writeLock().unlock();
      }

    }

잠시 볼 것이 AnnotatedMethod는 메소드와 메소드 파라미터를 가지고 있고

 private final Method method;
  private final Method bridgedMethod;
  private final MethodParameter[] parameters;

이것을 상속한 HandlerMethod는 빈과 관련된 정보를 가짐

public class HandlerMethod extends AnnotatedMethod {
  protected static final Log logger = LogFactory.getLog(HandlerMethod.class);
  private final Object bean;
  @Nullable
  private final BeanFactory beanFactory;

또 다시 이것을 상속한 것이 InvocalbeHandlerMethod 이건 호출이 가능한 클래스
아무튼 메소드 정보를 담음
애플리케이션을 실행하면 이렇게 초기화 과정이 실행되고
특정 RequestMapping으로 요청을 하면 다시 실행되어
InvocableHandlerMethod 에서 getMethodArgumentValues 이 함수 실행하면서 파라미터를 넣어준다

profile
풀스택 자바 개발자입니다

0개의 댓글