From 25131ebf6f572598d17f3391318c7fe55a16c7b3 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Tue, 13 Jul 2021 11:44:06 +0100 Subject: [PATCH] Resource handler initialized only once Closes gh-27153 --- .../config/ResourceHandlerRegistry.java | 32 +++++++++------ .../annotation/ResourceHandlerRegistry.java | 41 ++++++++++--------- 2 files changed, 41 insertions(+), 32 deletions(-) diff --git a/spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistry.java b/spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistry.java index bcdff7fa8aff..86f3729f7161 100644 --- a/spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistry.java +++ b/spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2018 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -28,6 +28,7 @@ import org.springframework.lang.Nullable; import org.springframework.web.reactive.handler.AbstractUrlHandlerMapping; import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping; +import org.springframework.web.reactive.resource.ResourceTransformer; import org.springframework.web.reactive.resource.ResourceTransformerSupport; import org.springframework.web.reactive.resource.ResourceUrlProvider; import org.springframework.web.reactive.resource.ResourceWebHandler; @@ -136,23 +137,28 @@ protected AbstractUrlHandlerMapping getHandlerMapping() { } Map urlMap = new LinkedHashMap<>(); for (ResourceHandlerRegistration registration : this.registrations) { + ResourceWebHandler handler = getRequestHandler(registration); for (String pathPattern : registration.getPathPatterns()) { - ResourceWebHandler handler = registration.getRequestHandler(); - handler.getResourceTransformers().forEach(transformer -> { - if (transformer instanceof ResourceTransformerSupport) { - ((ResourceTransformerSupport) transformer).setResourceUrlProvider(this.resourceUrlProvider); - } - }); - try { - handler.afterPropertiesSet(); - } - catch (Throwable ex) { - throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex); - } urlMap.put(pathPattern, handler); } } return new SimpleUrlHandlerMapping(urlMap, this.order); } + private ResourceWebHandler getRequestHandler(ResourceHandlerRegistration registration) { + ResourceWebHandler handler = registration.getRequestHandler(); + for (ResourceTransformer transformer : handler.getResourceTransformers()) { + if (transformer instanceof ResourceTransformerSupport) { + ((ResourceTransformerSupport) transformer).setResourceUrlProvider(this.resourceUrlProvider); + } + } + try { + handler.afterPropertiesSet(); + } + catch (Throwable ex) { + throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex); + } + return handler; + } + } diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java index cd4645fd32f5..ba710151595d 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2020 the original author or authors. + * Copyright 2002-2021 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -159,35 +159,38 @@ public ResourceHandlerRegistry setOrder(int order) { * of no registrations. */ @Nullable - @SuppressWarnings("deprecation") protected AbstractHandlerMapping getHandlerMapping() { if (this.registrations.isEmpty()) { return null; } - Map urlMap = new LinkedHashMap<>(); for (ResourceHandlerRegistration registration : this.registrations) { + ResourceHttpRequestHandler handler = getRequestHandler(registration); for (String pathPattern : registration.getPathPatterns()) { - 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); - } urlMap.put(pathPattern, handler); } } - return new SimpleUrlHandlerMapping(urlMap, this.order); } + @SuppressWarnings("deprecation") + private ResourceHttpRequestHandler getRequestHandler(ResourceHandlerRegistration registration) { + 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); + } + return handler; + } + }