This commit is contained in:
Rossen Stoyanchev
2017-06-22 18:33:11 -04:00
parent ac68cc35c2
commit 782c595cf7
3 changed files with 41 additions and 27 deletions

View File

@@ -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<String> 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;
}
}

View File

@@ -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<Object> getInterceptors() {
return this.registrations.stream()
.sorted(OrderComparator.INSTANCE)
.sorted(INTERCEPTOR_ORDER_COMPARATOR)
.map(InterceptorRegistration::getInterceptor)
.collect(Collectors.toList());
}
private static final Comparator<Object> INTERCEPTOR_ORDER_COMPARATOR =
OrderComparator.INSTANCE.withSourceProvider(object -> {
if (object instanceof InterceptorRegistration) {
return (Ordered) ((InterceptorRegistration) object)::getOrder;
}
return null;
});
}

View File

@@ -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<Object> interceptors = registry.getInterceptors();
List<Object> 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<Object> interceptors = registry.getInterceptors();
List<Object> 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));
}
}