PageableHandlerMethodArgumentResolver Cusomize 하기 by 완태 - woowacourse-teams/2021-gpu-is-mine GitHub Wiki
PageableHandlerMethodArgumentResolver 구현 분석
public class PageableHandlerMethodArgumentResolver extends PageableHandlerMethodArgumentResolverSupport
implements PageableArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return Pageable.class.equals(parameter.getParameterType());
}
@Override
public Pageable resolveArgument(생략..)
Pageable class를 가진 parameter가 왔을 때, resolveArgument Logic 실행.
PageableHandlerMethodArgumentResolverSupport
클래스 내부에서 pageable 로직이 정의되어 있음.
@PageDefault Annotation으로 설정하기
PageableHandlerMethodArgumentResolverSupport
에 정의된 getDefaultFromAnnotationOrFallback
에서 @PageableDefault라는 어노테이션이 되었을 때, 그 설정으로 진행된다.
Spring에서 Pageable의 기본 설정은 어떻게 되어있을까?
Spring의 Auto-Congiration에서는 다음의 로직이 포함되어 있음
public class SpringDataWebAutoConfiguration {
private final SpringDataWebProperties properties;
public SpringDataWebAutoConfiguration(SpringDataWebProperties properties) {
this.properties = properties;
}
@Bean
@ConditionalOnMissingBean
public PageableHandlerMethodArgumentResolverCustomizer pageableCustomizer() {
return (resolver) -> {
Pageable pageable = this.properties.getPageable();
resolver.setPageParameterName(pageable.getPageParameter());
resolver.setSizeParameterName(pageable.getSizeParameter());
resolver.setOneIndexedParameters(pageable.isOneIndexedParameters());
resolver.setPrefix(pageable.getPrefix());
resolver.setQualifierDelimiter(pageable.getQualifierDelimiter());
resolver.setFallbackPageable(PageRequest.of(0, pageable.getDefaultPageSize()));
resolver.setMaxPageSize(pageable.getMaxPageSize());
};
}
@Bean
@ConditionalOnMissingBean
public SortHandlerMethodArgumentResolverCustomizer sortCustomizer() {
return (resolver) -> resolver.setSortParameter(this.properties.getSort().getSortParameter());
}
}
@ConditionalOnMissingBean
설정의 경우, 해담 Bean(PageableHandlerMethodArgumentResolverCustomizer
) 생성되지 않게 되면 여기서 정의한 Bean이 BeanFactory에 생성되게끔 작업되어 있음.
SpringDataWebProperties
클래스 내부적으로 기본값이 설정되어 있고(하나의 Page의 크기 = 20) 이는 @ConfigurationProperties("spring.data.web")
를 가르키고 있기 때문에, application-properties에 별도로 spring.data.web.pageable.default-page-size=100
와 같은 설정을 추가해주면, 이 설정으로 변경됨.
CustomConfiguration 적용하기
만약 별도의 CustomPageableConfiguration을 만들어서 Bean값을 정의하게 되면, SpringDataWebAutoConfiguration
에서는 추가적인 Bean생성이 되지 않기 때문에, Custom한 값으로 적용되게 된다. 아래의 경우 DefualtPage를 Integer.MAX_VALUE를 적용한 경우다.
@Configuration
public class CustomPageableConfiguration {
@Bean
public PageableHandlerMethodArgumentResolverCustomizer customize() {
return p -> p.setFallbackPageable(PageRequest.of(0, Integer.MAX_VALUE));
}
}
두가지 모두를 적용해서 사용해 봤을 때,
CustomPageableConfiguration
이 적용된 것을 확인할 수 있었다.SpringDataWebAutoConfiguration
보다CustomPageableConfiguration
이 먼저 적용이 되는 것을 유추할 수 있다. (다른 Configuration들의 경우도 우리가 Custom하게 적용했을 때, 그 설정을 따르는 경우가 많았는데, 그 내부적인 구현도@ConditionalOnMissingBean
으로 설정되지 않았을까 유추해본다.