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
This commit is contained in:
Phillip Webb 2014-10-10 13:09:30 -07:00
parent a1bcd70377
commit a3527521a2
2 changed files with 22 additions and 4 deletions

View File

@ -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
.<Resource> asList(new ClassPathResource("/")));
requestHandler.setLocations(getLocations());
return requestHandler;
}
private List<Resource> getLocations() {
List<Resource> locations = new ArrayList<Resource>(
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);
}
}
}