From 3c887f631940a0492e607ac6f885aff7be6b4fa4 Mon Sep 17 00:00:00 2001 From: Oliver Drotbohm Date: Thu, 26 Aug 2021 16:13:44 +0200 Subject: [PATCH] Avoid reflection in SpringDataWebConfiguration. We now use an explicit lookup via the application context to consume PageableHandlerMethodArgumentResolverCustomizer and SortHandlerMethodArgumentResolverCustomizer rather than autowiring those into a field. Fixes #2440. --- .../config/SpringDataWebConfiguration.java | 20 ++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/src/main/java/org/springframework/data/web/config/SpringDataWebConfiguration.java b/src/main/java/org/springframework/data/web/config/SpringDataWebConfiguration.java index bebbaea32..ad1ab33c5 100644 --- a/src/main/java/org/springframework/data/web/config/SpringDataWebConfiguration.java +++ b/src/main/java/org/springframework/data/web/config/SpringDataWebConfiguration.java @@ -16,11 +16,9 @@ package org.springframework.data.web.config; import java.util.List; -import java.util.Optional; import org.springframework.beans.factory.BeanClassLoaderAware; import org.springframework.beans.factory.ObjectFactory; -import org.springframework.beans.factory.annotation.Autowired; import org.springframework.beans.factory.annotation.Qualifier; import org.springframework.context.ApplicationContext; import org.springframework.context.annotation.Bean; @@ -66,9 +64,8 @@ public class SpringDataWebConfiguration implements WebMvcConfigurer, BeanClassLo private final Lazy sortResolver; private final Lazy pageableResolver; - - private @Autowired Optional pageableResolverCustomizer; - private @Autowired Optional sortResolverCustomizer; + private final Lazy pageableResolverCustomizer; + private final Lazy sortResolverCustomizer; public SpringDataWebConfiguration(ApplicationContext context, @Qualifier("mvcConversionService") ObjectFactory conversionService) { @@ -77,10 +74,15 @@ public class SpringDataWebConfiguration implements WebMvcConfigurer, BeanClassLo Assert.notNull(conversionService, "ConversionService must not be null!"); this.context = context; + this.conversionService = conversionService; this.sortResolver = Lazy.of(() -> context.getBean("sortResolver", SortHandlerMethodArgumentResolver.class)); - this.pageableResolver = Lazy - .of(() -> context.getBean("pageableResolver", PageableHandlerMethodArgumentResolver.class)); + this.pageableResolver = Lazy.of( // + () -> context.getBean("pageableResolver", PageableHandlerMethodArgumentResolver.class)); + this.pageableResolverCustomizer = Lazy.of( // + () -> context.getBeanProvider(PageableHandlerMethodArgumentResolverCustomizer.class).getIfAvailable()); + this.sortResolverCustomizer = Lazy.of( // + () -> context.getBeanProvider(SortHandlerMethodArgumentResolverCustomizer.class).getIfAvailable()); } /* @@ -181,11 +183,11 @@ public class SpringDataWebConfiguration implements WebMvcConfigurer, BeanClassLo } protected void customizePageableResolver(PageableHandlerMethodArgumentResolver pageableResolver) { - pageableResolverCustomizer.ifPresent(c -> c.customize(pageableResolver)); + pageableResolverCustomizer.getOptional().ifPresent(c -> c.customize(pageableResolver)); } protected void customizeSortResolver(SortHandlerMethodArgumentResolver sortResolver) { - sortResolverCustomizer.ifPresent(c -> c.customize(sortResolver)); + sortResolverCustomizer.getOptional().ifPresent(c -> c.customize(sortResolver)); } private void forwardBeanClassLoader(BeanClassLoaderAware target) {