Skip to content

Commit f913940

Browse files
committed
Avoid unnecessary getMappingForMethod repeat (in particular for RequestMappingInfo)
Issue. SPR-11428
1 parent 72fe7eb commit f913940

File tree

1 file changed

+15
-10
lines changed

1 file changed

+15
-10
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/handler/AbstractHandlerMethodMapping.java

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.util.Collection;
2222
import java.util.Collections;
2323
import java.util.Comparator;
24+
import java.util.IdentityHashMap;
2425
import java.util.LinkedHashMap;
2526
import java.util.List;
2627
import java.util.Map;
@@ -126,17 +127,26 @@ protected void detectHandlerMethods(final Object handler) {
126127
Class<?> handlerType =
127128
(handler instanceof String ? getApplicationContext().getType((String) handler) : handler.getClass());
128129

130+
// Avoid repeated calls to getMappingForMethod which would rebuild RequestMatchingInfo instances
131+
final Map<Method, T> mappings = new IdentityHashMap<Method, T>();
129132
final Class<?> userType = ClassUtils.getUserClass(handlerType);
133+
130134
Set<Method> methods = HandlerMethodSelector.selectMethods(userType, new MethodFilter() {
131135
@Override
132136
public boolean matches(Method method) {
133-
return getMappingForMethod(method, userType) != null;
137+
T mapping = getMappingForMethod(method, userType);
138+
if (mapping != null) {
139+
mappings.put(method, mapping);
140+
return true;
141+
}
142+
else {
143+
return false;
144+
}
134145
}
135146
});
136147

137148
for (Method method : methods) {
138-
T mapping = getMappingForMethod(method, userType);
139-
registerHandlerMethod(handler, method, mapping);
149+
registerHandlerMethod(handler, method, mappings.get(method));
140150
}
141151
}
142152

@@ -243,25 +253,21 @@ protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Ex
243253
*/
244254
protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletRequest request) throws Exception {
245255
List<Match> matches = new ArrayList<Match>();
246-
247256
List<T> directPathMatches = this.urlMap.get(lookupPath);
248257
if (directPathMatches != null) {
249258
addMatchingMappings(directPathMatches, matches, request);
250259
}
251-
252260
if (matches.isEmpty()) {
253-
// No choice but to go through all mappings
261+
// No choice but to go through all mappings...
254262
addMatchingMappings(this.handlerMethods.keySet(), matches, request);
255263
}
256264

257265
if (!matches.isEmpty()) {
258266
Comparator<Match> comparator = new MatchComparator(getMappingComparator(request));
259267
Collections.sort(matches, comparator);
260-
261268
if (logger.isTraceEnabled()) {
262269
logger.trace("Found " + matches.size() + " matching mapping(s) for [" + lookupPath + "] : " + matches);
263270
}
264-
265271
Match bestMatch = matches.get(0);
266272
if (matches.size() > 1) {
267273
Match secondBestMatch = matches.get(1);
@@ -273,7 +279,6 @@ protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletReques
273279
m1 + ", " + m2 + "}");
274280
}
275281
}
276-
277282
handleMatch(bestMatch.mapping, lookupPath, request);
278283
return bestMatch.handlerMethod;
279284
}
@@ -286,7 +291,7 @@ private void addMatchingMappings(Collection<T> mappings, List<Match> matches, Ht
286291
for (T mapping : mappings) {
287292
T match = getMatchingMapping(mapping, request);
288293
if (match != null) {
289-
matches.add(new Match(match, handlerMethods.get(mapping)));
294+
matches.add(new Match(match, this.handlerMethods.get(mapping)));
290295
}
291296
}
292297
}

0 commit comments

Comments
 (0)