From 782c595cf7f839d2d6bf4c6bfc0502fc8c2c4ae0 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Thu, 22 Jun 2017 18:33:11 -0400 Subject: [PATCH] Polish --- .../annotation/InterceptorRegistration.java | 30 +++++++++---------- .../annotation/InterceptorRegistry.java | 13 +++++++- .../annotation/InterceptorRegistryTests.java | 25 +++++++++------- 3 files changed, 41 insertions(+), 27 deletions(-) diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java index 5bd6ff8f9b..1e98bc9d3a 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistration.java @@ -34,7 +34,7 @@ import org.springframework.web.servlet.handler.MappedInterceptor; * @author Keith Donald * @since 3.1 */ -public class InterceptorRegistration implements Ordered { +public class InterceptorRegistration { private final HandlerInterceptor interceptor; @@ -42,9 +42,10 @@ public class InterceptorRegistration implements Ordered { private final List excludePatterns = new ArrayList<>(); + private int order = 0; + private PathMatcher pathMatcher; - private int order = 0; /** * Creates an {@link InterceptorRegistration} instance. @@ -70,6 +71,18 @@ public class InterceptorRegistration implements Ordered { return this; } + /** + * An order position to be used, default is 0. + */ + public InterceptorRegistration order(int order){ + this.order = order; + return this; + } + + protected int getOrder() { + return this.order; + } + /** * A PathMatcher implementation to use with this interceptor. This is an optional, * advanced property required only if using custom PathMatcher implementations @@ -101,17 +114,4 @@ public class InterceptorRegistration implements Ordered { return mappedInterceptor; } - /** - * An order position to be used, default is 0. - */ - public InterceptorRegistration order(int order){ - this.order = order; - return this; - } - - @Override - public int getOrder() { - return order; - } - } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistry.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistry.java index 0e4f4357eb..a5196205e4 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistry.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/InterceptorRegistry.java @@ -17,10 +17,12 @@ package org.springframework.web.servlet.config.annotation; import java.util.ArrayList; +import java.util.Comparator; import java.util.List; import java.util.stream.Collectors; import org.springframework.core.OrderComparator; +import org.springframework.core.Ordered; import org.springframework.web.context.request.WebRequestInterceptor; import org.springframework.web.servlet.HandlerInterceptor; import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapter; @@ -67,9 +69,18 @@ public class InterceptorRegistry { */ protected List getInterceptors() { return this.registrations.stream() - .sorted(OrderComparator.INSTANCE) + .sorted(INTERCEPTOR_ORDER_COMPARATOR) .map(InterceptorRegistration::getInterceptor) .collect(Collectors.toList()); } + + private static final Comparator INTERCEPTOR_ORDER_COMPARATOR = + OrderComparator.INSTANCE.withSourceProvider(object -> { + if (object instanceof InterceptorRegistration) { + return (Ordered) ((InterceptorRegistration) object)::getOrder; + } + return null; + }); + } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java index 1c586d0aaf..4eab8a4ece 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/config/annotation/InterceptorRegistryTests.java @@ -40,7 +40,10 @@ import org.springframework.web.servlet.handler.WebRequestHandlerInterceptorAdapt import org.springframework.web.servlet.i18n.LocaleChangeInterceptor; import org.springframework.web.servlet.theme.ThemeChangeInterceptor; -import static org.junit.Assert.*; +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertSame; +import static org.junit.Assert.assertTrue; +import static org.junit.Assert.fail; /** * Test fixture with a {@link InterceptorRegistry}, two {@link HandlerInterceptor}s and two @@ -198,25 +201,25 @@ public class InterceptorRegistryTests { @Test public void orderedInterceptors() throws Exception { - registry.addInterceptor(interceptor1).order(Ordered.LOWEST_PRECEDENCE); - registry.addInterceptor(interceptor2).order(Ordered.HIGHEST_PRECEDENCE); + this.registry.addInterceptor(this.interceptor1).order(Ordered.LOWEST_PRECEDENCE); + this.registry.addInterceptor(this.interceptor2).order(Ordered.HIGHEST_PRECEDENCE); - List interceptors = registry.getInterceptors(); + List interceptors = this.registry.getInterceptors(); assertEquals(2, interceptors.size()); - assertSame(interceptor2, interceptors.get(0)); - assertSame(interceptor1, interceptors.get(1)); + assertSame(this.interceptor2, interceptors.get(0)); + assertSame(this.interceptor1, interceptors.get(1)); } @Test public void nonOrderedInterceptors() throws Exception { - registry.addInterceptor(interceptor1).order(0); - registry.addInterceptor(interceptor2).order(0); + this.registry.addInterceptor(this.interceptor1).order(0); + this.registry.addInterceptor(this.interceptor2).order(0); - List interceptors = registry.getInterceptors(); + List interceptors = this.registry.getInterceptors(); assertEquals(2, interceptors.size()); - assertSame(interceptor1, interceptors.get(0)); - assertSame(interceptor2, interceptors.get(1)); + assertSame(this.interceptor1, interceptors.get(0)); + assertSame(this.interceptor2, interceptors.get(1)); } }