mirror of
https://github.com/spring-projects/spring-boot.git
synced 2024-09-03 04:26:12 +08:00
Add spring.resources.cachePeriod to MvcAutoConfiguration
Fixes gh-232
This commit is contained in:
parent
5627caa724
commit
25cc68cafe
@ -122,6 +122,9 @@ public class WebMvcAutoConfiguration {
|
||||
@Value("${spring.view.suffix:}")
|
||||
private String suffix = "";
|
||||
|
||||
@Value("${spring.resources.cachePeriod:}")
|
||||
private Integer cachePeriod;
|
||||
|
||||
@Autowired
|
||||
private ListableBeanFactory beanFactory;
|
||||
|
||||
@ -193,12 +196,14 @@ public class WebMvcAutoConfiguration {
|
||||
@Override
|
||||
public void addResourceHandlers(ResourceHandlerRegistry registry) {
|
||||
if (!registry.hasMappingForPattern("/webjars/**")) {
|
||||
registry.addResourceHandler("/webjars/**").addResourceLocations(
|
||||
"classpath:/META-INF/resources/webjars/");
|
||||
registry.addResourceHandler("/webjars/**")
|
||||
.addResourceLocations("classpath:/META-INF/resources/webjars/")
|
||||
.setCachePeriod(this.cachePeriod);
|
||||
}
|
||||
if (!registry.hasMappingForPattern("/**")) {
|
||||
registry.addResourceHandler("/**").addResourceLocations(
|
||||
RESOURCE_LOCATIONS);
|
||||
registry.addResourceHandler("/**")
|
||||
.addResourceLocations(RESOURCE_LOCATIONS)
|
||||
.setCachePeriod(this.cachePeriod);
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -208,7 +208,9 @@ public class AutoConfigurationReportLoggingInitializerTests {
|
||||
}
|
||||
|
||||
@Configuration
|
||||
@Import({ WebMvcAutoConfiguration.class, HttpMessageConvertersAutoConfiguration.class })
|
||||
@Import({ WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class })
|
||||
static class Config {
|
||||
|
||||
}
|
||||
|
@ -21,6 +21,7 @@ import java.util.List;
|
||||
|
||||
import org.junit.After;
|
||||
import org.junit.Test;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.HttpMessageConvertersAutoConfiguration;
|
||||
import org.springframework.boot.autoconfigure.web.WebMvcAutoConfiguration;
|
||||
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
|
||||
@ -78,7 +79,8 @@ public class DeviceResolverAutoConfigurationTests {
|
||||
AnnotationConfigEmbeddedWebApplicationContext context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
DeviceResolverAutoConfiguration.class);
|
||||
DeviceResolverAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
context.refresh();
|
||||
RequestMappingHandlerMapping mapping = (RequestMappingHandlerMapping) context
|
||||
.getBean("requestMappingHandlerMapping");
|
||||
|
@ -28,6 +28,7 @@ import org.junit.After;
|
||||
import org.junit.Rule;
|
||||
import org.junit.Test;
|
||||
import org.junit.rules.ExpectedException;
|
||||
import org.springframework.boot.autoconfigure.PropertyPlaceholderAutoConfiguration;
|
||||
import org.springframework.boot.context.embedded.AnnotationConfigEmbeddedWebApplicationContext;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerCustomizerBeanPostProcessor;
|
||||
import org.springframework.boot.context.embedded.EmbeddedServletContainerFactory;
|
||||
@ -78,7 +79,8 @@ public class WebMvcAutoConfigurationTests {
|
||||
public void handerAdaptersCreated() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class);
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertEquals(3, this.context.getBeanNamesForType(HandlerAdapter.class).length);
|
||||
assertFalse(this.context.getBean(RequestMappingHandlerAdapter.class)
|
||||
@ -92,7 +94,8 @@ public class WebMvcAutoConfigurationTests {
|
||||
public void handerMappingsCreated() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class);
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
assertEquals(6, this.context.getBeanNamesForType(HandlerMapping.class).length);
|
||||
}
|
||||
@ -101,7 +104,8 @@ public class WebMvcAutoConfigurationTests {
|
||||
public void resourceHandlerMapping() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
this.context.register(Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class);
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
Map<String, List<Resource>> mappingLocations = getMappingLocations();
|
||||
assertThat(mappingLocations.get("/**").size(), equalTo(5));
|
||||
@ -114,7 +118,8 @@ public class WebMvcAutoConfigurationTests {
|
||||
public void resourceHandlerMappingOverrideWebjars() throws Exception {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
this.context.register(WebJars.class, Config.class, WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class);
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
Map<String, List<Resource>> mappingLocations = getMappingLocations();
|
||||
assertThat(mappingLocations.get("/webjars/**").size(), equalTo(1));
|
||||
@ -127,7 +132,8 @@ public class WebMvcAutoConfigurationTests {
|
||||
this.context = new AnnotationConfigEmbeddedWebApplicationContext();
|
||||
this.context.register(AllResources.class, Config.class,
|
||||
WebMvcAutoConfiguration.class,
|
||||
HttpMessageConvertersAutoConfiguration.class);
|
||||
HttpMessageConvertersAutoConfiguration.class,
|
||||
PropertyPlaceholderAutoConfiguration.class);
|
||||
this.context.refresh();
|
||||
Map<String, List<Resource>> mappingLocations = getMappingLocations();
|
||||
assertThat(mappingLocations.get("/**").size(), equalTo(1));
|
||||
|
Loading…
Reference in New Issue
Block a user