21
21
import java .util .Collection ;
22
22
import java .util .Collections ;
23
23
import java .util .Comparator ;
24
+ import java .util .IdentityHashMap ;
24
25
import java .util .LinkedHashMap ;
25
26
import java .util .List ;
26
27
import java .util .Map ;
@@ -126,17 +127,26 @@ protected void detectHandlerMethods(final Object handler) {
126
127
Class <?> handlerType =
127
128
(handler instanceof String ? getApplicationContext ().getType ((String ) handler ) : handler .getClass ());
128
129
130
+ // Avoid repeated calls to getMappingForMethod which would rebuild RequestMatchingInfo instances
131
+ final Map <Method , T > mappings = new IdentityHashMap <Method , T >();
129
132
final Class <?> userType = ClassUtils .getUserClass (handlerType );
133
+
130
134
Set <Method > methods = HandlerMethodSelector .selectMethods (userType , new MethodFilter () {
131
135
@ Override
132
136
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
+ }
134
145
}
135
146
});
136
147
137
148
for (Method method : methods ) {
138
- T mapping = getMappingForMethod (method , userType );
139
- registerHandlerMethod (handler , method , mapping );
149
+ registerHandlerMethod (handler , method , mappings .get (method ));
140
150
}
141
151
}
142
152
@@ -243,25 +253,21 @@ protected HandlerMethod getHandlerInternal(HttpServletRequest request) throws Ex
243
253
*/
244
254
protected HandlerMethod lookupHandlerMethod (String lookupPath , HttpServletRequest request ) throws Exception {
245
255
List <Match > matches = new ArrayList <Match >();
246
-
247
256
List <T > directPathMatches = this .urlMap .get (lookupPath );
248
257
if (directPathMatches != null ) {
249
258
addMatchingMappings (directPathMatches , matches , request );
250
259
}
251
-
252
260
if (matches .isEmpty ()) {
253
- // No choice but to go through all mappings
261
+ // No choice but to go through all mappings...
254
262
addMatchingMappings (this .handlerMethods .keySet (), matches , request );
255
263
}
256
264
257
265
if (!matches .isEmpty ()) {
258
266
Comparator <Match > comparator = new MatchComparator (getMappingComparator (request ));
259
267
Collections .sort (matches , comparator );
260
-
261
268
if (logger .isTraceEnabled ()) {
262
269
logger .trace ("Found " + matches .size () + " matching mapping(s) for [" + lookupPath + "] : " + matches );
263
270
}
264
-
265
271
Match bestMatch = matches .get (0 );
266
272
if (matches .size () > 1 ) {
267
273
Match secondBestMatch = matches .get (1 );
@@ -273,7 +279,6 @@ protected HandlerMethod lookupHandlerMethod(String lookupPath, HttpServletReques
273
279
m1 + ", " + m2 + "}" );
274
280
}
275
281
}
276
-
277
282
handleMatch (bestMatch .mapping , lookupPath , request );
278
283
return bestMatch .handlerMethod ;
279
284
}
@@ -286,7 +291,7 @@ private void addMatchingMappings(Collection<T> mappings, List<Match> matches, Ht
286
291
for (T mapping : mappings ) {
287
292
T match = getMatchingMapping (mapping , request );
288
293
if (match != null ) {
289
- matches .add (new Match (match , handlerMethods .get (mapping )));
294
+ matches .add (new Match (match , this . handlerMethods .get (mapping )));
290
295
}
291
296
}
292
297
}
0 commit comments