From a19a56541074cf891a58f5bffe5e2357c880ebea Mon Sep 17 00:00:00 2001 From: Andy Wilkinson Date: Tue, 29 Sep 2020 11:08:09 +0100 Subject: [PATCH] Stop registering the default servlet by default Previously, the default servlet was registered automatically when using embedded Jetty, Tomcat, or Undertow. However, it is not used by the majority of applications where Spring MVC's DispatcherServlet will be the only servlet that's needed. As such configuring the default servlet was wasting CPU and memory. This commit changes the default for registering the default servlet to false. It can be re-enabled by setting server.servlet.register-default-servlet=true. Closes gh-22915 --- .../boot/autoconfigure/web/ServerProperties.java | 2 +- ...JerseyAutoConfigurationCustomFilterContextPathTests.java | 6 +++--- .../JerseyAutoConfigurationCustomFilterPathTests.java | 5 +++-- .../JerseyAutoConfigurationDefaultFilterPathTests.java | 5 +++-- .../remote/client/RemoteClientConfigurationTests.java | 6 ++++-- .../web/servlet/server/AbstractServletWebServerFactory.java | 4 ++-- .../servlet/server/ConfigurableServletWebServerFactory.java | 6 +++--- .../server/AbstractServletWebServerFactoryTests.java | 3 +++ .../src/main/resources/application.properties | 1 + 9 files changed, 23 insertions(+), 15 deletions(-) create mode 100644 spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-traditional/src/main/resources/application.properties diff --git a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java index 9740226d8c..0212155007 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java @@ -235,7 +235,7 @@ public class ServerProperties { /** * Whether to register the default Servlet with the container. */ - private boolean registerDefaultServlet = true; + private boolean registerDefaultServlet = false; @NestedConfigurationProperty private final Encoding encoding = new Encoding(); diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java index 308be4f186..d563060ec3 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,8 +50,8 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Dave Syer */ -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, - properties = { "spring.jersey.type=filter", "server.servlet.context-path=/app" }) +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.jersey.type=filter", + "server.servlet.context-path=/app", "server.servlet.register-default-servlet=true" }) @DirtiesContext class JerseyAutoConfigurationCustomFilterContextPathTests { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterPathTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterPathTests.java index 5ef1b34603..4efb451a82 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterPathTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterPathTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -50,7 +50,8 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Dave Syer */ -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.jersey.type=filter") +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, + properties = { "spring.jersey.type=filter", "server.servlet.register-default-servlet=true" }) @DirtiesContext class JerseyAutoConfigurationCustomFilterPathTests { diff --git a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationDefaultFilterPathTests.java b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationDefaultFilterPathTests.java index afe01f326a..7484bb7a3f 100644 --- a/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationDefaultFilterPathTests.java +++ b/spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationDefaultFilterPathTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -49,7 +49,8 @@ import static org.assertj.core.api.Assertions.assertThat; * * @author Dave Syer */ -@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.jersey.type=filter") +@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, + properties = { "spring.jersey.type=filter", "server.servlet.register-default-servlet=true" }) @DirtiesContext class JerseyAutoConfigurationDefaultFilterPathTests { diff --git a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/remote/client/RemoteClientConfigurationTests.java b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/remote/client/RemoteClientConfigurationTests.java index e1b006b1a6..5b3f909043 100644 --- a/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/remote/client/RemoteClientConfigurationTests.java +++ b/spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/remote/client/RemoteClientConfigurationTests.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -156,7 +156,9 @@ class RemoteClientConfigurationTests { @Bean TomcatServletWebServerFactory tomcat() { - return new TomcatServletWebServerFactory(0); + TomcatServletWebServerFactory webServerFactory = new TomcatServletWebServerFactory(0); + webServerFactory.setRegisterDefaultServlet(true); + return webServerFactory; } @Bean diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactory.java index 9cddc7198f..38e6e29fca 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -65,7 +65,7 @@ public abstract class AbstractServletWebServerFactory extends AbstractConfigurab private Session session = new Session(); - private boolean registerDefaultServlet = true; + private boolean registerDefaultServlet = false; private MimeMappings mimeMappings = new MimeMappings(MimeMappings.DEFAULT); diff --git a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/ConfigurableServletWebServerFactory.java b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/ConfigurableServletWebServerFactory.java index c60503c298..a2cdd5264a 100644 --- a/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/ConfigurableServletWebServerFactory.java +++ b/spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/ConfigurableServletWebServerFactory.java @@ -1,5 +1,5 @@ /* - * Copyright 2012-2019 the original author or authors. + * Copyright 2012-2020 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -66,8 +66,8 @@ public interface ConfigurableServletWebServerFactory extends ConfigurableWebServ void setSession(Session session); /** - * Set if the DefaultServlet should be registered. Defaults to {@code true} so that - * files from the {@link #setDocumentRoot(File) document root} will be served. + * Set if the DefaultServlet should be registered. Defaults to {@code false} since + * 2.4. * @param registerDefaultServlet if the default servlet should be registered */ void setRegisterDefaultServlet(boolean registerDefaultServlet); diff --git a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java index c51f1f0f19..4800bf83e0 100644 --- a/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java +++ b/spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java @@ -378,6 +378,7 @@ public abstract class AbstractServletWebServerFactoryTests { void mimeType() throws Exception { FileCopyUtils.copy("test", new FileWriter(new File(this.tempDir, "test.xxcss"))); AbstractServletWebServerFactory factory = getFactory(); + factory.setRegisterDefaultServlet(true); factory.setDocumentRoot(this.tempDir); MimeMappings mimeMappings = new MimeMappings(); mimeMappings.add("xxcss", "text/css"); @@ -544,6 +545,7 @@ public abstract class AbstractServletWebServerFactoryTests { @Test void sslNeedsClientAuthenticationSucceedsWithClientCertificate() throws Exception { AbstractServletWebServerFactory factory = getFactory(); + factory.setRegisterDefaultServlet(true); addTestTxtFile(factory); factory.setSsl(getSsl(ClientAuth.NEED, "password", "classpath:test.jks", "classpath:test.jks", null, null)); this.webServer = factory.getWebServer(); @@ -1200,6 +1202,7 @@ public abstract class AbstractServletWebServerFactoryTests { private void addTestTxtFile(AbstractServletWebServerFactory factory) throws IOException { FileCopyUtils.copy("test", new FileWriter(new File(this.tempDir, "test.txt"))); factory.setDocumentRoot(this.tempDir); + factory.setRegisterDefaultServlet(true); } protected String getLocalUrl(String resourcePath) { diff --git a/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-traditional/src/main/resources/application.properties b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-traditional/src/main/resources/application.properties new file mode 100644 index 0000000000..c4ac87775c --- /dev/null +++ b/spring-boot-tests/spring-boot-smoke-tests/spring-boot-smoke-test-traditional/src/main/resources/application.properties @@ -0,0 +1 @@ +server.servlet.register-default-servlet=true \ No newline at end of file