diff --git a/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java b/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java index 8cbcc61ec9f..c895e25528d 100644 --- a/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java +++ b/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatServerProperties.java @@ -697,7 +697,7 @@ public class TomcatServerProperties { /** * Maximum size of the static resource cache. */ - private @Nullable DataSize cacheMaxSize; + private DataSize cacheMaxSize = DataSize.ofMegabytes(10); /** * Time-to-live of the static resource cache. @@ -712,11 +712,11 @@ public class TomcatServerProperties { this.allowCaching = allowCaching; } - public @Nullable DataSize getCacheMaxSize() { + public DataSize getCacheMaxSize() { return this.cacheMaxSize; } - public void setCacheMaxSize(@Nullable DataSize cacheMaxSize) { + public void setCacheMaxSize(DataSize cacheMaxSize) { this.cacheMaxSize = cacheMaxSize; } diff --git a/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java b/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java index ca835acb471..b067915ae70 100644 --- a/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java +++ b/module/spring-boot-tomcat/src/main/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizer.java @@ -380,14 +380,12 @@ public class TomcatWebServerFactoryCustomizer factory.addContextCustomizers((context) -> context.addLifecycleListener((event) -> { if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) { context.getResources().setCachingAllowed(resource.isAllowCaching()); + long cacheMaxSize = resource.getCacheMaxSize().toKilobytes(); + context.getResources().setCacheMaxSize(cacheMaxSize); if (resource.getCacheTtl() != null) { long ttl = resource.getCacheTtl().toMillis(); context.getResources().setCacheTtl(ttl); } - if (resource.getCacheMaxSize() != null) { - long cacheMaxSize = resource.getCacheMaxSize().toKilobytes(); - context.getResources().setCacheMaxSize(cacheMaxSize); - } } })); } diff --git a/module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java b/module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java index 8b8c203b529..8f9e9446db3 100644 --- a/module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java +++ b/module/spring-boot-tomcat/src/test/java/org/springframework/boot/tomcat/autoconfigure/TomcatWebServerFactoryCustomizerTests.java @@ -322,6 +322,18 @@ class TomcatWebServerFactoryCustomizerTests { assertThat(remoteIpValve.getTrustedProxies()).isEqualTo("proxy1|proxy2"); } + @Test + void resourceCacheMatchesDefault() { + TomcatServerProperties properties = new TomcatServerProperties(); + customizeAndRunServer((server) -> { + Tomcat tomcat = server.getTomcat(); + Context context = (Context) tomcat.getHost().findChildren()[0]; + assertThat(properties.getResource().isAllowCaching()).isEqualTo(context.getResources().isCachingAllowed()); + assertThat(properties.getResource().getCacheMaxSize()) + .isEqualTo(DataSize.ofKilobytes(context.getResources().getCacheMaxSize())); + }); + } + @Test void customStaticResourceAllowCaching() { bind("server.tomcat.resource.allow-caching=false"); @@ -334,7 +346,7 @@ class TomcatWebServerFactoryCustomizerTests { @Test void customStaticResourceCacheMaxSize() { - bind("server.tomcat.resource.cache-max-size=4096KB"); + bind("server.tomcat.resource.cache-max-size=4MB"); customizeAndRunServer((server) -> { Tomcat tomcat = server.getTomcat(); Context context = (Context) tomcat.getHost().findChildren()[0];