Add spring.resources.cachePeriod to MvcAutoConfiguration

Fixes gh-232
This commit is contained in:
Christopher Smith 2014-01-17 12:09:23 -06:00 committed by Dave Syer
parent 5627caa724
commit 25cc68cafe
4 changed files with 26 additions and 11 deletions

View File

@ -122,6 +122,9 @@ public class WebMvcAutoConfiguration {
@Value("${spring.view.suffix:}") @Value("${spring.view.suffix:}")
private String suffix = ""; private String suffix = "";
@Value("${spring.resources.cachePeriod:}")
private Integer cachePeriod;
@Autowired @Autowired
private ListableBeanFactory beanFactory; private ListableBeanFactory beanFactory;
@ -193,12 +196,14 @@ public class WebMvcAutoConfiguration {
@Override @Override
public void addResourceHandlers(ResourceHandlerRegistry registry) { public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!registry.hasMappingForPattern("/webjars/**")) { if (!registry.hasMappingForPattern("/webjars/**")) {
registry.addResourceHandler("/webjars/**").addResourceLocations( registry.addResourceHandler("/webjars/**")
"classpath:/META-INF/resources/webjars/"); .addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(this.cachePeriod);
} }
if (!registry.hasMappingForPattern("/**")) { if (!registry.hasMappingForPattern("/**")) {
registry.addResourceHandler("/**").addResourceLocations( registry.addResourceHandler("/**")
RESOURCE_LOCATIONS); .addResourceLocations(RESOURCE_LOCATIONS)
.setCachePeriod(this.cachePeriod);
} }
} }

View File

@ -208,7 +208,9 @@ public class AutoConfigurationReportLoggingInitializerTests {
} }
@Configuration @Configuration
@Import({ WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class }) @Import({ WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class })
static class Config { static class Config {
} }

View File

@ -21,6 +21,7 @@ import java.util.List;
import org.junit.After; import org.junit.After;
import org.junit.Test; import org.junit.Test;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration; import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration; import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
@ -78,7 +79,8 @@ public class DeviceResolverAutoConfigurationTests {
AnnotationConfigEmbeddedWebApplicationContext context = new AnnotationConfigEmbeddedWebApplicationContext(); AnnotationConfigEmbeddedWebApplicationContext context = new AnnotationConfigEmbeddedWebApplicationContext();
context.register(Config.class, WebMvcAutoConfiguration.class, context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class,
DeviceResolverAutoConfiguration.class); DeviceResolverAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
context.refresh(); context.refresh();
RequestMappingHandlerMapping mapping = (RequestMappingHandlerMapping) context RequestMappingHandlerMapping mapping = (RequestMappingHandlerMapping) context
.getBean("requestMappingHandlerMapping"); .getBean("requestMappingHandlerMapping");

View File

@ -28,6 +28,7 @@ import org.junit.After;
import org.junit.Rule; import org.junit.Rule;
import org.junit.Test; import org.junit.Test;
import org.junit.rules.ExpectedException; import org.junit.rules.ExpectedException;
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext; import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor; import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory; import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
@ -78,7 +79,8 @@ public class WebMvcAutoConfigurationTests {
public void handerAdaptersCreated() throws Exception { public void handerAdaptersCreated() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, WebMvcAutoConfiguration.class, this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class); HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertEquals(3, this.context.getBeanNamesForType(HandlerAdapter.class).length); assertEquals(3, this.context.getBeanNamesForType(HandlerAdapter.class).length);
assertFalse(this.context.getBean(RequestMappingHandlerAdapter.class) assertFalse(this.context.getBean(RequestMappingHandlerAdapter.class)
@ -92,7 +94,8 @@ public class WebMvcAutoConfigurationTests {
public void handerMappingsCreated() throws Exception { public void handerMappingsCreated() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, WebMvcAutoConfiguration.class, this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class); HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
assertEquals(6, this.context.getBeanNamesForType(HandlerMapping.class).length); assertEquals(6, this.context.getBeanNamesForType(HandlerMapping.class).length);
} }
@ -101,7 +104,8 @@ public class WebMvcAutoConfigurationTests {
public void resourceHandlerMapping() throws Exception { public void resourceHandlerMapping() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(Config.class, WebMvcAutoConfiguration.class, this.context.register(Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class); HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
Map<String, List<Resource>> mappingLocations = getMappingLocations(); Map<String, List<Resource>> mappingLocations = getMappingLocations();
assertThat(mappingLocations.get("/**").size(), equalTo(5)); assertThat(mappingLocations.get("/**").size(), equalTo(5));
@ -114,7 +118,8 @@ public class WebMvcAutoConfigurationTests {
public void resourceHandlerMappingOverrideWebjars() throws Exception { public void resourceHandlerMappingOverrideWebjars() throws Exception {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(WebJars.class, Config.class, WebMvcAutoConfiguration.class, this.context.register(WebJars.class, Config.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class); HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
Map<String, List<Resource>> mappingLocations = getMappingLocations(); Map<String, List<Resource>> mappingLocations = getMappingLocations();
assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1)); assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1));
@ -127,7 +132,8 @@ public class WebMvcAutoConfigurationTests {
this.context = new AnnotationConfigEmbeddedWebApplicationContext(); this.context = new AnnotationConfigEmbeddedWebApplicationContext();
this.context.register(AllResources.class, Config.class, this.context.register(AllResources.class, Config.class,
WebMvcAutoConfiguration.class, WebMvcAutoConfiguration.class,
HttpMessageConvertersAutoConfiguration.class); HttpMessageConvertersAutoConfiguration.class,
PropertyPlaceholderAutoConfiguration.class);
this.context.refresh(); this.context.refresh();
Map<String, List<Resource>> mappingLocations = getMappingLocations(); Map<String, List<Resource>> mappingLocations = getMappingLocations();
assertThat(mappingLocations.get("/**").size(), equalTo(1)); assertThat(mappingLocations.get("/**").size(), equalTo(1));