Spring Boot is powerful yet flexible. It tries to auto-configure most of the stuff for you so that you can get up and running quickly with your application.
It uses sensible defaults during auto-configuration but also gives you the flexibility to change those defaults by just tweaking a few things.
In this article, you’ll learn about the most common configuration tweaks that might need to do in your application.
Spring Boot uses Tomcat as the default embedded server. If you wanna use some other popular server like Jetty or Undertow then you just need to exclude tomcat dependency and add the other server dependency.
<!-- Exclude tomcat dependency --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- Include jetty dependency --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-jetty</artifactId> </dependency>
<!-- Exclude tomcat dependency --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!-- Include undertow dependency --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-undertow</artifactId> </dependency>
By default, Spring Boot runs your application on port
8080 with the context path
If you wanna change the default port and context path, then it’s just a matter of specifying the corresponding values in the
application.properties file -
# HTTP Server port server.port=8080 # Make the application accessible on the given context path (http://localhost:8080/myapp) server.servlet.context-path=/myapp
GZip compression is a very simple and effective way to save bandwidth and improve the speed of your website.
It reduces the response time of your website by compressing the resources and then sending it over to the clients. It saves bandwidth by at least 50%.
GZip compression is disabled by default in Spring Boot. To enable it, add the following properties to your
application.properties file -
Note that, GZip compression has a small overhead. Therefore I’ve added a
min-response-sizeproperty to tell spring boot server to compress the response only if the size is more than the given value.
HTTP/2 is an improvement over the HTTP1 protocol. It improves the page load speed of your website by employing several mechanisms like data compression, server push, multiplexing of multiple requests over a single TCP connection etc.
You can enable HTTP2 in spring boot with the following property, if the server has support for it -
# Enable HTTP/2 support, if the current environment supports it server.http2.enabled=true
Browser caching is another way to improve the page load speed of your website. You can set
cache-control headers to tell browsers to cache static resources until a certain period of time.
Browser caching is disabled by default in Spring Boot. You can enable caching by setting the following properties in the
# Maximum time the response should be cached (in seconds) spring.resources.cache.cachecontrol.max-age=120 # The cache must re-validate stale resources with the server. Any expired resources must not be used without re-validating. spring.resources.cache.cachecontrol.must-revalidate=true
Following are few other cache related properties that you should be aware of -
# The resources are private and intended for a single user. They must not be stored by a shared cache (e.g CDN). spring.resources.cache.cachecontrol.cache-private= # set a boolean value true/false # The resources are public and any cache may store the response. spring.resources.cache.cachecontrol.cache-public= # set a boolean value true/false
Multipart file uploads are enabled by default in Spring Boot with the following property -
But there are few other default multipart properties that you might need to change.
By default, Spring Boot allows you to upload a file with a maximum size of
1MB. You might need to change this to the desired value as per your requirements.
Following is the complete set of properties -
# Write files to disk if the file size is more than 2KB. spring.servlet.multipart.file-size-threshold=2KB # The intermediate disk location where the uploaded files are written spring.servlet.multipart.location=/tmp # Maximum file size that can be uploaded spring.servlet.multipart.max-file-size=50MB # Maximum allowed multipart request size spring.servlet.multipart.max-request-size=75MB
In this short article, you learned how to tweak Spring Boot’s default configurations as per your needs. You can find a full index of common application properties on the following official Spring Boot page -
Spring Boot Common application properties
Whenever you need to configure anything in your spring boot application, the first thing you should do is - check the above common application properties page. Most probably, you’ll find a property for configuring the stuff that you’re looking for.
You might also wanna check out the following articles on Spring Boot configuration -
本文分享自微信公众号 - 黑洞日志（heidcloud）
原文出处及转载信息见文内详细说明，如有侵权，请联系 firstname.lastname@example.org 删除。