【官方文档】Spring Boot 2.0 迁移指南 已翻译 100%
This document is meant to help you migrate your application to Spring Boot 2.0 by providing thematic sections that mirror the developer guide.
Before you start
First, Spring Boot 2.0 requires Java 8 or later. Java 6 and 7 are no longer supported.
With Spring Boot 2.0, many configuration properties were renamed/removed and developers need to update their
application.yml accordingly. To help you with that, Spring Boot ships a new
spring-boot-properties-migrator module. Once added as a dependency to your project, this will not only analyze your application’s environment and print diagnostics at startup, but also temporarily migrate properties at runtime for you. This is a must have during your application migration:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-properties-migrator</artifactId> </dependency>
|Once you’re done with the migration, please make sure to remove this module from your project’s dependencies.|
If you wish to look into specifics, here’s a curated list of resources - otherwise, proceed to the next sections:
Building your Spring Boot application
Spring Boot Maven plugin
The plugin configuration attributes that are exposed as properties now all start with a
spring-bootprefix for consistency and to avoid clashes with other plugins.
For instance, the following command enables the
foo profile using the command line:
mvn spring-boot:run -Dspring-boot.run.profiles=foo
Custom include/exclude patterns have been aligned to latest Surefire’s defaults. If you were relying on ours, update your plugin configuration accordingly. They used to be as follows:
<plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-surefire-plugin</artifactId> <configuration> <includes> <include>**/*Tests.java</include> <include>**/*Test.java</include> </includes> <excludes> <exclude>**/Abstract*.java</exclude> </excludes> </configuration> </plugin>
|If you are using JUnit 5, you should downgrade Surefire to |
Spring Boot Gradle Plugin
Spring Boot’s Gradle plugin no longer automatically applies the dependency management plugin. Instead, Spring Boot’s plugin now reacts to the dependency management plugin being applied by importing the correct version of the spring-boot-dependencies bom. This gives you more control over how and when dependency management is configured.
For most applications applying the dependency management plugin will be sufficient:
apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' // <-- add this to your build.gradle
|The dependency management plugin remains a transitive dependency of spring-boot-gradle-plugin so there’s no need for it to be listed as a classpath dependency in your buildscript configuration.|
Building executable jars and wars
bootRepackage task has been replaced with
bootWar tasks for building executable jars and wars respectively.
Spring Boot Features
Default Proxying strategy
Spring Boot now uses CGLIB proxying by default, including for the AOP support. If you need proxy-based proxy, you’ll need to set the
Spring Boot applications can now operates in more modes so
spring.main.web-environmentproperty is now deprecated in favor of
spring.main.web-application-type that provides more control.
If you want to make sure an application doesn’t start a web server you’d have to change the property to:
|there is also a |
Spring Boot application events changes
We’ve added a new event,
ApplicationStartedEvent is sent after the context has been refreshed but before any application and command-line runners have been called.
ApplicationReadyEvent is sent after any application and command-line runners have been called. It indicates that the application is ready to service requests.
See the updated reference documentation.
In our effort to limit the number of root namespaces that Spring Boot uses, banner-related properties have been relocated to
The rules related to relaxed binding have been tightened. Let’s assume an existing
All prefixes must be in kebab-case format (lower-case, hyphen separated),
acme.my_projectare invalid - you must use
Property names can use kebab-case (
my-name), camel-case (
myName) or snake-case (
Environment properties (from the OS environment variables) must use the usual upper case underscore format where the underscore should only be used to separate parts of the key, ACME_MYPROJECT_MYNAME
This new relaxed bindings as several advantages:
There is no need to worry about the structure of the key in
@ConditionalOnProperty: as long as the key is defined in the canonical format, the supported relaxed variants will work transparently. If you were using the
prefixattribute you can now simply put the full key using the
RelaxedPropertyResolveris no longer available as the
Environmenttakes care of that automatically:
env.getProperty("com.foo.my-bar")will find a
org.springframework.boot.bind package is no longer available and is replaced by the new relaxed binding infrastructure. In particular,
RelaxedDataBinder and friends have been replaced with a new
Binder API. The following samples binds a POJO from the
new Binder(ConfigurationPropertySources.from(propertySource)) .bind("com.foo", Bindable.of(target)))
It is now mandatory that your
@ConfigurationProperties object is annotated with
@Validated if you want to turn on validation.
The behavior of the
spring.config.location configuration has been fixed; it previously added a location to the list of default ones, now it replaces the default locations. If you were relying on the way it was handled previously, you should now use
Developing Web Applications
Embedded containers package structure
In order to support reactive use cases, the embedded containers package structure has been refactored quite extensively.
EmbeddedServletContainer has been renamed to
WebServer and the
org.springframework.boot.context.embedded package has been relocated to
org.springframework.boot.web.embedded. For instance, if you were customizing the embedded Tomcat container using the
TomcatEmbeddedServletContainerFactory callback interface, you should now use
Servlet-specific server properties
A number of
server.* properties that are Servlet-specific have moved to
|Old property||New property|
Web Starter as a transitive dependency
Previously several Spring Boot starters were transitively depending on Spring MVC with
spring-boot-starter-web. With the new support of Spring WebFlux,
spring-boot-starter-thymeleaf are not depending on it anymore. It is the developer’s responsibility to choose and add
Mustache templates default file extension
The default file extension for Mustache templates was
.html, it is now
.mustache to align with the official spec and most IDE plugins. You can override this new default by changing the
spring.mustache.suffix configuration key.
Jackson / JSON support
In 2.0, we’ve flipped a Jackson configuration default to write JSR-310 dates as ISO-8601 strings. If you wish to return to the previous behavior, you can add
spring.jackson.serialization.write-dates-as-timestamps=true to your configuration.
spring-boot-starter-json starter gathers the necessary bits to read and write JSON. It provides not only
jackson-databind but also useful modules when working with Java8:
jackson-module-parameter-names. If you were manually depending on those modules, you can now depend on this new starter instead.
Spring MVC path matching default behavior change
We’ve decided to change the default for suffix path matching in Spring MVC applications (see #11105). This feature is not enabled by default anymore, following a best practice documented in Spring Framework.
If your application expects requests like
"GET /projects/spring-boot.json" to be mapped to
@GetMapping("/projects/spring-boot") mappings, this change is affecting you.
For more information about this and how to mitigate that change, check out the reference documentation about path matching and content negotiation in Spring Boot.
The default dispatcher types for a Servlet Filter are now
DipatcherType.REQUEST; this aligns Spring Boot’s default with the Servlet specification’s default. If you wish to map a filter to other dispatcher types, please register your Filter using a
|Spring Security and Spring Session filters are configured for |
requestFactory(ClientHttpRequestFactory) method has been replaced by a new
requestFactory(Supplier<ClientHttpRequestFactory> requestFactorySupplier) method. The use of a
Supplier allows every template produced by the builder to use its own request factory, thereby avoiding side-effects that can be caused by sharing a factory. See #11255.
Spring Boot 1.x used and provided dependency management for
webjars-locator is a "poorly named library … that wraps the
webjars-locator-core project". Dependencies on
org.webjars:webjars-locator should be updated to use