Skip to content

Commit a19a565

Browse files
committed
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
1 parent 2852ea0 commit a19a565

File tree

9 files changed

+23
-15
lines changed

9 files changed

+23
-15
lines changed

spring-boot-project/spring-boot-autoconfigure/src/main/java/org/springframework/boot/autoconfigure/web/ServerProperties.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ public static class Servlet {
235235
/**
236236
* Whether to register the default Servlet with the container.
237237
*/
238-
private boolean registerDefaultServlet = true;
238+
private boolean registerDefaultServlet = false;
239239

240240
@NestedConfigurationProperty
241241
private final Encoding encoding = new Encoding();

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterContextPathTests.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,8 +50,8 @@
5050
*
5151
* @author Dave Syer
5252
*/
53-
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
54-
properties = { "spring.jersey.type=filter", "server.servlet.context-path=/app" })
53+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = { "spring.jersey.type=filter",
54+
"server.servlet.context-path=/app", "server.servlet.register-default-servlet=true" })
5555
@DirtiesContext
5656
class JerseyAutoConfigurationCustomFilterContextPathTests {
5757

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationCustomFilterPathTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -50,7 +50,8 @@
5050
*
5151
* @author Dave Syer
5252
*/
53-
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.jersey.type=filter")
53+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
54+
properties = { "spring.jersey.type=filter", "server.servlet.register-default-servlet=true" })
5455
@DirtiesContext
5556
class JerseyAutoConfigurationCustomFilterPathTests {
5657

spring-boot-project/spring-boot-autoconfigure/src/test/java/org/springframework/boot/autoconfigure/jersey/JerseyAutoConfigurationDefaultFilterPathTests.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -49,7 +49,8 @@
4949
*
5050
* @author Dave Syer
5151
*/
52-
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT, properties = "spring.jersey.type=filter")
52+
@SpringBootTest(webEnvironment = WebEnvironment.RANDOM_PORT,
53+
properties = { "spring.jersey.type=filter", "server.servlet.register-default-servlet=true" })
5354
@DirtiesContext
5455
class JerseyAutoConfigurationDefaultFilterPathTests {
5556

spring-boot-project/spring-boot-devtools/src/test/java/org/springframework/boot/devtools/remote/client/RemoteClientConfigurationTests.java

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -156,7 +156,9 @@ static class Config {
156156

157157
@Bean
158158
TomcatServletWebServerFactory tomcat() {
159-
return new TomcatServletWebServerFactory(0);
159+
TomcatServletWebServerFactory webServerFactory = new TomcatServletWebServerFactory(0);
160+
webServerFactory.setRegisterDefaultServlet(true);
161+
return webServerFactory;
160162
}
161163

162164
@Bean

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactory.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -65,7 +65,7 @@ public abstract class AbstractServletWebServerFactory extends AbstractConfigurab
6565

6666
private Session session = new Session();
6767

68-
private boolean registerDefaultServlet = true;
68+
private boolean registerDefaultServlet = false;
6969

7070
private MimeMappings mimeMappings = new MimeMappings(MimeMappings.DEFAULT);
7171

spring-boot-project/spring-boot/src/main/java/org/springframework/boot/web/servlet/server/ConfigurableServletWebServerFactory.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright 2012-2019 the original author or authors.
2+
* Copyright 2012-2020 the original author or authors.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -66,8 +66,8 @@ public interface ConfigurableServletWebServerFactory extends ConfigurableWebServ
6666
void setSession(Session session);
6767

6868
/**
69-
* Set if the DefaultServlet should be registered. Defaults to {@code true} so that
70-
* files from the {@link #setDocumentRoot(File) document root} will be served.
69+
* Set if the DefaultServlet should be registered. Defaults to {@code false} since
70+
* 2.4.
7171
* @param registerDefaultServlet if the default servlet should be registered
7272
*/
7373
void setRegisterDefaultServlet(boolean registerDefaultServlet);

spring-boot-project/spring-boot/src/test/java/org/springframework/boot/web/servlet/server/AbstractServletWebServerFactoryTests.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,7 @@ void documentRoot() throws Exception {
378378
void mimeType() throws Exception {
379379
FileCopyUtils.copy("test", new FileWriter(new File(this.tempDir, "test.xxcss")));
380380
AbstractServletWebServerFactory factory = getFactory();
381+
factory.setRegisterDefaultServlet(true);
381382
factory.setDocumentRoot(this.tempDir);
382383
MimeMappings mimeMappings = new MimeMappings();
383384
mimeMappings.add("xxcss", "text/css");
@@ -544,6 +545,7 @@ void pkcs12KeyStoreAndTrustStore() throws Exception {
544545
@Test
545546
void sslNeedsClientAuthenticationSucceedsWithClientCertificate() throws Exception {
546547
AbstractServletWebServerFactory factory = getFactory();
548+
factory.setRegisterDefaultServlet(true);
547549
addTestTxtFile(factory);
548550
factory.setSsl(getSsl(ClientAuth.NEED, "password", "classpath:test.jks", "classpath:test.jks", null, null));
549551
this.webServer = factory.getWebServer();
@@ -1200,6 +1202,7 @@ protected void service(HttpServletRequest req, HttpServletResponse resp) throws
12001202
private void addTestTxtFile(AbstractServletWebServerFactory factory) throws IOException {
12011203
FileCopyUtils.copy("test", new FileWriter(new File(this.tempDir, "test.txt")));
12021204
factory.setDocumentRoot(this.tempDir);
1205+
factory.setRegisterDefaultServlet(true);
12031206
}
12041207

12051208
protected String getLocalUrl(String resourcePath) {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
server.servlet.register-default-servlet=true

0 commit comments

Comments
 (0)