diff --git a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java index c2feaaf075cf..44cfea267e2b 100644 --- a/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java +++ b/spring-webmvc/src/main/java/org/springframework/web/servlet/resource/ResourceUrlProvider.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -129,6 +129,9 @@ public void onApplicationEvent(ContextRefreshedEvent event) { if (this.handlerMap.isEmpty() && logger.isDebugEnabled()) { logger.debug("No resource handling mappings found"); } + if(!this.handlerMap.isEmpty()) { + this.autodetect = false; + } } } diff --git a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlProviderTests.java b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlProviderTests.java index beb043d113b1..d504d6820b25 100644 --- a/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlProviderTests.java +++ b/spring-webmvc/src/test/java/org/springframework/web/servlet/resource/ResourceUrlProviderTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2002-2014 the original author or authors. + * Copyright 2002-2015 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. @@ -21,11 +21,17 @@ import java.util.List; import java.util.Map; +import org.hamcrest.Matchers; import org.junit.Before; import org.junit.Test; +import org.springframework.context.annotation.Bean; +import org.springframework.context.annotation.Configuration; import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.Resource; +import org.springframework.mock.web.test.MockServletContext; +import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; +import org.springframework.web.servlet.handler.SimpleUrlHandlerMapping; import static org.junit.Assert.*; @@ -88,4 +94,34 @@ private void initTranslator() { this.translator.setHandlerMap(this.handlerMap); } + // SPR-12592 + @Test + public void initializeOnce() throws Exception { + AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext(); + context.setServletContext(new MockServletContext()); + context.register(HandlerMappingConfiguration.class); + context.refresh(); + ResourceUrlProvider translator = context.getBean(ResourceUrlProvider.class); + assertThat(translator.getHandlerMap(), Matchers.hasKey("/resources/**")); + assertFalse(translator.isAutodetect()); + } + + @Configuration + public static class HandlerMappingConfiguration { + @Bean + public SimpleUrlHandlerMapping simpleUrlHandlerMapping() { + ResourceHttpRequestHandler handler = new ResourceHttpRequestHandler(); + HashMap handlerMap = new HashMap(); + handlerMap.put("/resources/**", handler); + SimpleUrlHandlerMapping hm = new SimpleUrlHandlerMapping(); + hm.setUrlMap(handlerMap); + return hm; + } + + @Bean + public ResourceUrlProvider resourceUrlProvider() { + return new ResourceUrlProvider(); + } + } + }