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
This commit is contained in:
Andy Wilkinson 2020-09-29 11:08:09 +01:00
parent 2852ea0808
commit a19a565410
9 changed files with 23 additions and 15 deletions

View File

@ -235,7 +235,7 @@ public class ServerProperties {
/** /**
* Whether to register the default Servlet with the container. * Whether to register the default Servlet with the container.
*/ */
private boolean registerDefaultServlet = true; private boolean registerDefaultServlet = false;
@NestedConfigurationProperty @NestedConfigurationProperty
private final Encoding encoding = new Encoding(); private final Encoding encoding = new Encoding();

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * @author Dave Syer
*/ */
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, @SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.jersey.type=filter",
properties = { "spring.jersey.type=filter", "server.servlet.context-path=/app" }) "server.servlet.context-path=/app", "server.servlet.register-default-servlet=true" })
@DirtiesContext @DirtiesContext
class JerseyAutoConfigurationCustomFilterContextPathTests { class JerseyAutoConfigurationCustomFilterContextPathTests {

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * @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 @DirtiesContext
class JerseyAutoConfigurationCustomFilterPathTests { class JerseyAutoConfigurationCustomFilterPathTests {

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 * @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 @DirtiesContext
class JerseyAutoConfigurationDefaultFilterPathTests { class JerseyAutoConfigurationDefaultFilterPathTests {

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -156,7 +156,9 @@ class RemoteClientConfigurationTests {
@Bean @Bean
TomcatServletWebServerFactory tomcat() { TomcatServletWebServerFactory tomcat() {
return new TomcatServletWebServerFactory(0); TomcatServletWebServerFactory webServerFactory = new TomcatServletWebServerFactory(0);
webServerFactory.setRegisterDefaultServlet(true);
return webServerFactory;
} }
@Bean @Bean

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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 Session session = new Session();
private boolean registerDefaultServlet = true; private boolean registerDefaultServlet = false;
private MimeMappings mimeMappings = new MimeMappings(MimeMappings.DEFAULT); private MimeMappings mimeMappings = new MimeMappings(MimeMappings.DEFAULT);

View File

@ -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"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with 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); void setSession(Session session);
/** /**
* Set if the DefaultServlet should be registered. Defaults to {@code true} so that * Set if the DefaultServlet should be registered. Defaults to {@code false} since
* files from the {@link #setDocumentRoot(File) document root} will be served. * 2.4.
* @param registerDefaultServlet if the default servlet should be registered * @param registerDefaultServlet if the default servlet should be registered
*/ */
void setRegisterDefaultServlet(boolean registerDefaultServlet); void setRegisterDefaultServlet(boolean registerDefaultServlet);

View File

@ -378,6 +378,7 @@ public abstract class AbstractServletWebServerFactoryTests {
void mimeType() throws Exception { void mimeType() throws Exception {
FileCopyUtils.copy("test", new FileWriter(new File(this.tempDir, "test.xxcss"))); FileCopyUtils.copy("test", new FileWriter(new File(this.tempDir, "test.xxcss")));
AbstractServletWebServerFactory factory = getFactory(); AbstractServletWebServerFactory factory = getFactory();
factory.setRegisterDefaultServlet(true);
factory.setDocumentRoot(this.tempDir); factory.setDocumentRoot(this.tempDir);
MimeMappings mimeMappings = new MimeMappings(); MimeMappings mimeMappings = new MimeMappings();
mimeMappings.add("xxcss", "text/css"); mimeMappings.add("xxcss", "text/css");
@ -544,6 +545,7 @@ public abstract class AbstractServletWebServerFactoryTests {
@Test @Test
void sslNeedsClientAuthenticationSucceedsWithClientCertificate() throws Exception { void sslNeedsClientAuthenticationSucceedsWithClientCertificate() throws Exception {
AbstractServletWebServerFactory factory = getFactory(); AbstractServletWebServerFactory factory = getFactory();
factory.setRegisterDefaultServlet(true);
addTestTxtFile(factory); addTestTxtFile(factory);
factory.setSsl(getSsl(ClientAuth.NEED, "password", "classpath:test.jks", "classpath:test.jks", null, null)); factory.setSsl(getSsl(ClientAuth.NEED, "password", "classpath:test.jks", "classpath:test.jks", null, null));
this.webServer = factory.getWebServer(); this.webServer = factory.getWebServer();
@ -1200,6 +1202,7 @@ public abstract class AbstractServletWebServerFactoryTests {
private void addTestTxtFile(AbstractServletWebServerFactory factory) throws IOException { private void addTestTxtFile(AbstractServletWebServerFactory factory) throws IOException {
FileCopyUtils.copy("test", new FileWriter(new File(this.tempDir, "test.txt"))); FileCopyUtils.copy("test", new FileWriter(new File(this.tempDir, "test.txt")));
factory.setDocumentRoot(this.tempDir); factory.setDocumentRoot(this.tempDir);
factory.setRegisterDefaultServlet(true);
} }
protected String getLocalUrl(String resourcePath) { protected String getLocalUrl(String resourcePath) {

View File

@ -0,0 +1 @@
server.servlet.register-default-servlet=true