Skip to content

Commit 25131eb

Browse files
committed
Resource handler initialized only once
Closes gh-27153
1 parent 0267b00 commit 25131eb

File tree

2 files changed

+41
-32
lines changed

2 files changed

+41
-32
lines changed

spring-webflux/src/main/java/org/springframework/web/reactive/config/ResourceHandlerRegistry.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2018 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -28,6 +28,7 @@
2828
import org.springframework.lang.Nullable;
2929
import org.springframework.web.reactive.handler.AbstractUrlHandlerMapping;
3030
import org.springframework.web.reactive.handler.SimpleUrlHandlerMapping;
31+
import org.springframework.web.reactive.resource.ResourceTransformer;
3132
import org.springframework.web.reactive.resource.ResourceTransformerSupport;
3233
import org.springframework.web.reactive.resource.ResourceUrlProvider;
3334
import org.springframework.web.reactive.resource.ResourceWebHandler;
@@ -136,23 +137,28 @@ protected AbstractUrlHandlerMapping getHandlerMapping() {
136137
}
137138
Map<String, WebHandler> urlMap = new LinkedHashMap<>();
138139
for (ResourceHandlerRegistration registration : this.registrations) {
140+
ResourceWebHandler handler = getRequestHandler(registration);
139141
for (String pathPattern : registration.getPathPatterns()) {
140-
ResourceWebHandler handler = registration.getRequestHandler();
141-
handler.getResourceTransformers().forEach(transformer -> {
142-
if (transformer instanceof ResourceTransformerSupport) {
143-
((ResourceTransformerSupport) transformer).setResourceUrlProvider(this.resourceUrlProvider);
144-
}
145-
});
146-
try {
147-
handler.afterPropertiesSet();
148-
}
149-
catch (Throwable ex) {
150-
throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
151-
}
152142
urlMap.put(pathPattern, handler);
153143
}
154144
}
155145
return new SimpleUrlHandlerMapping(urlMap, this.order);
156146
}
157147

148+
private ResourceWebHandler getRequestHandler(ResourceHandlerRegistration registration) {
149+
ResourceWebHandler handler = registration.getRequestHandler();
150+
for (ResourceTransformer transformer : handler.getResourceTransformers()) {
151+
if (transformer instanceof ResourceTransformerSupport) {
152+
((ResourceTransformerSupport) transformer).setResourceUrlProvider(this.resourceUrlProvider);
153+
}
154+
}
155+
try {
156+
handler.afterPropertiesSet();
157+
}
158+
catch (Throwable ex) {
159+
throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
160+
}
161+
return handler;
162+
}
163+
158164
}

spring-webmvc/src/main/java/org/springframework/web/servlet/config/annotation/ResourceHandlerRegistry.java

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2020 the original author or authors.
2+
* Copyright 2002-2021 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -159,35 +159,38 @@ public ResourceHandlerRegistry setOrder(int order) {
159159
* of no registrations.
160160
*/
161161
@Nullable
162-
@SuppressWarnings("deprecation")
163162
protected AbstractHandlerMapping getHandlerMapping() {
164163
if (this.registrations.isEmpty()) {
165164
return null;
166165
}
167-
168166
Map<String, HttpRequestHandler> urlMap = new LinkedHashMap<>();
169167
for (ResourceHandlerRegistration registration : this.registrations) {
168+
ResourceHttpRequestHandler handler = getRequestHandler(registration);
170169
for (String pathPattern : registration.getPathPatterns()) {
171-
ResourceHttpRequestHandler handler = registration.getRequestHandler();
172-
if (this.pathHelper != null) {
173-
handler.setUrlPathHelper(this.pathHelper);
174-
}
175-
if (this.contentNegotiationManager != null) {
176-
handler.setContentNegotiationManager(this.contentNegotiationManager);
177-
}
178-
handler.setServletContext(this.servletContext);
179-
handler.setApplicationContext(this.applicationContext);
180-
try {
181-
handler.afterPropertiesSet();
182-
}
183-
catch (Throwable ex) {
184-
throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
185-
}
186170
urlMap.put(pathPattern, handler);
187171
}
188172
}
189-
190173
return new SimpleUrlHandlerMapping(urlMap, this.order);
191174
}
192175

176+
@SuppressWarnings("deprecation")
177+
private ResourceHttpRequestHandler getRequestHandler(ResourceHandlerRegistration registration) {
178+
ResourceHttpRequestHandler handler = registration.getRequestHandler();
179+
if (this.pathHelper != null) {
180+
handler.setUrlPathHelper(this.pathHelper);
181+
}
182+
if (this.contentNegotiationManager != null) {
183+
handler.setContentNegotiationManager(this.contentNegotiationManager);
184+
}
185+
handler.setServletContext(this.servletContext);
186+
handler.setApplicationContext(this.applicationContext);
187+
try {
188+
handler.afterPropertiesSet();
189+
}
190+
catch (Throwable ex) {
191+
throw new BeanInitializationException("Failed to init ResourceHttpRequestHandler", ex);
192+
}
193+
return handler;
194+
}
195+
193196
}

0 commit comments

Comments
 (0)