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.
*/
private boolean registerDefaultServlet = true;
private boolean registerDefaultServlet = false;
@NestedConfigurationProperty
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");
* 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 {

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");
* 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 {

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");
* 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 {

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");
* 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

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");
* 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);

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");
* 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);

View File

@ -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) {

View File

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