Skip to content

Commit f902fb9

Browse files
committed
Initialize ResourceUrlProvider only once
Prior to this change, the ResourceUrlProvider would listen to ContextRefreshedEvents and autodetect resource handlers each time. This can cause issues when multiple contexts are involved and the last one has no resource handler, thus clearing the previously detected ones. This commit disables resource handlers auto-detection once some have been detected with a refreshed context. Issue: SPR-12592
1 parent 77c8aa5 commit f902fb9

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -129,6 +129,9 @@ public void onApplicationEvent(ContextRefreshedEvent event) {
129129
if (this.handlerMap.isEmpty() && logger.isDebugEnabled()) {
130130
logger.debug("No resource handling mappings found");
131131
}
132+
if(!this.handlerMap.isEmpty()) {
133+
this.autodetect = false;
134+
}
132135
}
133136
}
134137

spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlProviderTests.java

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2002-2014 the original author or authors.
2+
* Copyright 2002-2015 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.
@@ -21,11 +21,17 @@
2121
import java.util.List;
2222
import java.util.Map;
2323

24+
import org.hamcrest.Matchers;
2425
import org.junit.Before;
2526
import org.junit.Test;
2627

28+
import org.springframework.context.annotation.Bean;
29+
import org.springframework.context.annotation.Configuration;
2730
import org.springframework.core.io.ClassPathResource;
2831
import org.springframework.core.io.Resource;
32+
import org.springframework.mock.web.test.MockServletContext;
33+
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
34+
import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping;
2935

3036
import static org.junit.Assert.*;
3137

@@ -88,4 +94,34 @@ private void initTranslator() {
8894
this.translator.setHandlerMap(this.handlerMap);
8995
}
9096

97+
// SPR-12592
98+
@Test
99+
public void initializeOnce() throws Exception {
100+
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
101+
context.setServletContext(new MockServletContext());
102+
context.register(HandlerMappingConfiguration.class);
103+
context.refresh();
104+
ResourceUrlProvider translator = context.getBean(ResourceUrlProvider.class);
105+
assertThat(translator.getHandlerMap(), Matchers.hasKey("/resources/**"));
106+
assertFalse(translator.isAutodetect());
107+
}
108+
109+
@Configuration
110+
public static class HandlerMappingConfiguration {
111+
@Bean
112+
public SimpleUrlHandlerMapping simpleUrlHandlerMapping() {
113+
ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler();
114+
HashMap<String, ResourceHttpRequestHandler> handlerMap = new HashMap<String, ResourceHttpRequestHandler>();
115+
handlerMap.put("/resources/**", handler);
116+
SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping();
117+
hm.setUrlMap(handlerMap);
118+
return hm;
119+
}
120+
121+
@Bean
122+
public ResourceUrlProvider resourceUrlProvider() {
123+
return new ResourceUrlProvider();
124+
}
125+
}
126+
91127
}

0 commit comments

Comments
 (0)