바인딩과 타입 변환

tokkaiiii·2025년 5월 4일

spring-mvc

목록 보기
17/27

DataBinder

: 요청 데이터를 객체로 바인딩한다, @ModelAttribute로 선언한 파라미터 처리

ConversionService

: 타입 변환에 특화된 서비스, 바인딩 중 필요한 경우 Converter로 타입변환한다

ModelAttributeMethodProcessor 에서
resolveArgument 메소드
Object attribute; 빈 객체를 생성한다
객체 필드 타입이 String이면 그냥 들어갈 수 있는데 int 나 객체면 타입 변환이 필요하다
AbstractNestablePropertyAccessor 클래스
processLocalProperty 메소드에서

valueToApply = this.convertForProperty(tokens.canonicalName, oldValue, originalValue, ph.toTypeDescriptor());

여기서 타입변환을 한다

 public <T> T convertIfNecessary(@Nullable String propertyName, @Nullable Object oldValue, @Nullable Object newValue, @Nullable Class<T> requiredType, @Nullable TypeDescriptor typeDescriptor) throws IllegalArgumentException {
    PropertyEditor editor = this.propertyEditorRegistry.findCustomEditor(requiredType, propertyName);
    ConversionFailedException conversionAttemptEx = null;
    ConversionService conversionService = this.propertyEditorRegistry.getConversionService();
    if (editor == null && conversionService != null && newValue != null && typeDescriptor != null) {
      TypeDescriptor sourceTypeDesc = TypeDescriptor.forObject(newValue);
      if (conversionService.canConvert(sourceTypeDesc, typeDescriptor)) {
        try {
          return (T)conversionService.convert(newValue, sourceTypeDesc, typeDescriptor);
        } catch (ConversionFailedException ex) {
          conversionAttemptEx = ex;
        }
      }
    }

String 이 들어오면 그냥 반환하면 되기 때문에 Source 그대로 반환하는 NoOpConverter 가 처리한다

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

0개의 댓글