Skip to content

Commit 6c00748

Browse files
committed
RepositoryRestConfigurer are again used in declared order.
4b36f79 introduced a regression by consuming registered RepositoryRestConfigurer instances via ApplicationContext.getBeansOfType(…).values() which, unlike the previous consumption via a List<RRC> in an @bean method, is losing the declared order of the RRC instances as ….getBeansOfType() is a Map. We now rather use an ObjectProvider and its ….orderedStream() method to consume the registered instances ordered properly. Fixes #1995.
1 parent b4b8fdf commit 6c00748

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

spring-data-rest-webmvc/src/main/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvcConfiguration.java

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import java.util.Map;
2424
import java.util.Optional;
2525
import java.util.Set;
26+
import java.util.stream.Collectors;
2627

2728
import org.springframework.beans.factory.BeanClassLoaderAware;
2829
import org.springframework.beans.factory.BeanFactoryUtils;
@@ -259,7 +260,10 @@ public RepositoryRestMvcConfiguration( //
259260
this.defaultConversionService = new DefaultFormattingConversionService();
260261

261262
this.configurerDelegate = Lazy.of(() -> {
262-
return new RepositoryRestConfigurerDelegate(context.getBeansOfType(RepositoryRestConfigurer.class).values());
263+
264+
return new RepositoryRestConfigurerDelegate(context.getBeanProvider(RepositoryRestConfigurer.class)
265+
.orderedStream()
266+
.collect(Collectors.toList()));
263267
});
264268

265269
this.repositoryRestConfiguration = Lazy.of(() -> context.getBean(RepositoryRestConfiguration.class));

spring-data-rest-webmvc/src/test/java/org/springframework/data/rest/webmvc/config/RepositoryRestMvConfigurationIntegrationTests.java

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,14 @@
2828
import org.junit.AfterClass;
2929
import org.junit.BeforeClass;
3030
import org.junit.Test;
31-
3231
import org.springframework.beans.factory.NoSuchBeanDefinitionException;
3332
import org.springframework.beans.factory.annotation.Autowired;
3433
import org.springframework.context.annotation.AnnotationConfigApplicationContext;
3534
import org.springframework.context.annotation.Bean;
3635
import org.springframework.context.annotation.Configuration;
3736
import org.springframework.context.annotation.Import;
3837
import org.springframework.context.support.AbstractApplicationContext;
38+
import org.springframework.core.annotation.Order;
3939
import org.springframework.core.convert.ConversionService;
4040
import org.springframework.data.domain.PageRequest;
4141
import org.springframework.data.domain.Sort.Direction;
@@ -45,6 +45,8 @@
4545
import org.springframework.data.rest.webmvc.RestMediaTypes;
4646
import org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter;
4747
import org.springframework.data.rest.webmvc.json.PersistentEntityJackson2Module;
48+
import org.springframework.data.util.Lazy;
49+
import org.springframework.data.util.Streamable;
4850
import org.springframework.data.web.HateoasPageableHandlerMethodArgumentResolver;
4951
import org.springframework.data.web.PageableHandlerMethodArgumentResolver;
5052
import org.springframework.hateoas.MediaTypes;
@@ -54,6 +56,7 @@
5456
import org.springframework.http.converter.HttpMessageConverter;
5557
import org.springframework.http.converter.json.AbstractJackson2HttpMessageConverter;
5658
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
59+
import org.springframework.test.util.ReflectionTestUtils;
5760
import org.springframework.util.MultiValueMap;
5861
import org.springframework.web.util.UriComponentsBuilder;
5962

@@ -192,6 +195,22 @@ public void hasConvertersForNamAndLdapName() {
192195
assertThat(service.canConvert(String.class, LdapName.class)).isTrue();
193196
}
194197

198+
@Test // #1995
199+
@SuppressWarnings("unchecked")
200+
public void registersRepositoryRestConfigurersInDeclaredOrder() {
201+
202+
RepositoryRestMvcConfiguration configuration = context.getBean(RepositoryRestMvcConfiguration.class);
203+
ExtendingConfiguration userConfig = context.getBean(ExtendingConfiguration.class);
204+
205+
Lazy<RepositoryRestConfigurerDelegate> delegate = (Lazy<RepositoryRestConfigurerDelegate>) ReflectionTestUtils
206+
.getField(configuration, "configurerDelegate");
207+
Iterable<RepositoryRestConfigurer> configurations = (Iterable<RepositoryRestConfigurer>) ReflectionTestUtils
208+
.getField(delegate.get(), "delegates");
209+
210+
assertThat(Streamable.of(configurations).toList())
211+
.containsSequence(userConfig.otherConfigurer(), userConfig.configurer());
212+
}
213+
195214
private static ObjectMapper getObjectMapper() {
196215

197216
AbstractJackson2HttpMessageConverter converter = context.getBean("halJacksonHttpMessageConverter",
@@ -214,6 +233,7 @@ CollectingComponent collectingComponent() {
214233
}
215234

216235
@Bean
236+
@Order(200)
217237
RepositoryRestConfigurer configurer() {
218238

219239
return RepositoryRestConfigurer.withConfig(config -> {
@@ -225,6 +245,12 @@ RepositoryRestConfigurer configurer() {
225245
config.setSortParamName("mySort");
226246
});
227247
}
248+
249+
@Bean
250+
@Order(100)
251+
RepositoryRestConfigurer otherConfigurer() {
252+
return RepositoryRestConfigurer.withConfig(__ -> {});
253+
}
228254
}
229255

230256
@Configuration

0 commit comments

Comments
 (0)