From a3527521a25eee700a2197264b7cbd24d0ef38b2 Mon Sep 17 00:00:00 2001 From: Phillip Webb Date: Fri, 10 Oct 2014 13:09:30 -0700 Subject: [PATCH] Also resolve favicon.ico from resource folders Update FaviconConfiguration to allow favicon.ico files to be resolved from static resource folders (/META-INF/resources, /resources, /static, /public) in addition to the root classpath. Fixes gh-1656 --- .../web/WebMvcAutoConfiguration.java | 26 +++++++++++++++--- .../main/resources/{ => static}/favicon.ico | Bin 2 files changed, 22 insertions(+), 4 deletions(-) rename spring-boot-samples/spring-boot-sample-web-ui/src/main/resources/{ => static}/favicon.ico (100%) diff --git a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java index 0f75bdaf4d..6933ce568e 100644 --- a/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java +++ b/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/WebMvcAutoConfiguration.java @@ -17,7 +17,7 @@ package org.springframework.boot.autoconfigure.web; import java.io.IOException; -import java.util.Arrays; +import java.util.ArrayList; import java.util.Collection; import java.util.Collections; import java.util.Date; @@ -39,6 +39,7 @@ import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty; import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication; import org.springframework.boot.context.properties.EnableConfigurationProperties; +import org.springframework.context.ResourceLoaderAware; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.core.Ordered; @@ -282,7 +283,9 @@ public class WebMvcAutoConfiguration { } @Configuration - public static class FaviconConfiguration { + public static class FaviconConfiguration implements ResourceLoaderAware { + + private ResourceLoader resourceLoader; @Bean public SimpleUrlHandlerMapping faviconHandlerMapping() { @@ -293,13 +296,28 @@ public class WebMvcAutoConfiguration { return mapping; } + @Override + public void setResourceLoader(ResourceLoader resourceLoader) { + this.resourceLoader = resourceLoader; + } + @Bean public ResourceHttpRequestHandler faviconRequestHandler() { ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler(); - requestHandler.setLocations(Arrays - . asList(new ClassPathResource("/"))); + requestHandler.setLocations(getLocations()); return requestHandler; } + + private List getLocations() { + List locations = new ArrayList( + CLASSPATH_RESOURCE_LOCATIONS.length + 1); + for (String location : CLASSPATH_RESOURCE_LOCATIONS) { + locations.add(this.resourceLoader.getResource(location)); + } + locations.add(new ClassPathResource("/")); + return Collections.unmodifiableList(locations); + } + } } diff --git a/spring-boot-samples/spring-boot-sample-web-ui/src/main/resources/favicon.ico b/spring-boot-samples/spring-boot-sample-web-ui/src/main/resources/static/favicon.ico similarity index 100% rename from spring-boot-samples/spring-boot-sample-web-ui/src/main/resources/favicon.ico rename to spring-boot-samples/spring-boot-sample-web-ui/src/main/resources/static/favicon.ico