Skip to content

ResourceHandlerRegistry#getHandlerMapping should initialize handler once in outer loop #27153

Closed
@lijinliangyihao

Description

@lijinliangyihao

Affects: 5.3.8


the code fragment below in method getHandlerMapping, line 168

    Map<String, HttpRequestHandler> urlMap = new LinkedHashMap<>();
    for (ResourceHandlerRegistration registration : this.registrations) {
	for (String pathPattern : registration.getPathPatterns()) {

            // 1. we get the 'handler' here from the outer loop variable 'registration', 
            //    set it's properties all with bean fields
            ResourceHttpRequestHandler handler = registration.getRequestHandler();
            if (this.pathHelper != null) {
	      handler.setUrlPathHelper(this.pathHelper);
            }
            if (this.contentNegotiationManager != null) {
	      handler.setContentNegotiationManager(this.contentNegotiationManager);
            }
            handler.setServletContext(this.servletContext);
            handler.setApplicationContext(this.applicationContext);
            try {
	      handler.afterPropertiesSet();
            }
            catch (Throwable ex) {
	      throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
            }

            // 2.  and here, we put it in a map, with inner loop's element as the key
            urlMap.put(pathPattern, handler);
	}
    }

Can't we move the code fragment in the inner loop between comment 1 and 2 to the outer loop?

Metadata

Metadata

Assignees

Labels

in: webIssues in web modules (web, webmvc, webflux, websocket)type: enhancementA general enhancement

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions