【官方文档】Spring Boot 2.0 迁移指南 已翻译 100%

oschina 投递于 2018/02/27 19:08 (共 24 段, 翻译完成于 03-12)
阅读 39277
收藏 51
15
加载中

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.properties/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>

Note

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:

已有 1 人翻译此段
我来翻译

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

Surefire defaults

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>

Tip

If you are using JUnit 5, you should downgrade Surefire to 2.19.1. The **/*Tests.java pattern is not included in this version so if you are relying on that, make sure to add it in your configuration.
已有 2 人翻译此段
我来翻译

Spring Boot Gradle Plugin

Spring Boot’s Gradle plugin has been largely rewritten to enable a number of significant improvements. You can read more about the plugin’s capabilities in its reference and apidocumentation.

Dependency management

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

Note

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

The bootRepackage task has been replaced with bootJar and bootWar tasks for building executable jars and wars respectively.

已有 1 人翻译此段
我来翻译

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.aop.proxy-target-class to false.

SpringApplication

Web Environment

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:

spring.main.web-application-type=none

Tip

there is also a setWebApplicationType on SpringApplication if you want to do that programmatically.

Spring Boot application events changes

We’ve added a new event, ApplicationStartedEventApplicationStartedEvent 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.

Banner

In our effort to limit the number of root namespaces that Spring Boot uses, banner-related properties have been relocated to spring.banner.

已有 1 人翻译此段
我来翻译

Externalized Configuration

Relaxed binding

The rules related to relaxed binding have been tightened. Let’s assume an existing acme.my-project.my-name property:

  1. All prefixes must be in kebab-case format (lower-case, hyphen separated), acme.myProject or acme.my_project are invalid - you must use acme.my-project here

  2. Property names can use kebab-case (my-name), camel-case (myName) or snake-case (my_name)

  3. 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

已有 1 人翻译此段
我来翻译

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 prefix attribute you can now simply put the full key using the name or value attributes

  • RelaxedPropertyResolver is no longer available as the Environment takes care of that automatically: env.getProperty("com.foo.my-bar") will find a com.foo.myBar property

The 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 com.foo prefix

new Binder(ConfigurationPropertySources.from(propertySource))
		.bind("com.foo", Bindable.of(target)))

@ConfigurationProperties validation

It is now mandatory that your @ConfigurationProperties object is annotated with @Validated if you want to turn on validation.

Configuration location

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 spring.config.additional-location instead.

已有 2 人翻译此段
我来翻译

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 TomcatServletWebServerFactory.

Servlet-specific server properties

A number of server.* properties that are Servlet-specific have moved to server.servlet:

Old propertyNew property

server.context-parameters.*

server.servlet.context-parameters.*

server.context-path

server.servlet.context-path

server.jsp.class-name

server.servlet.jsp.class-name

server.jsp.init-parameters.*

server.servlet.jsp.init-parameters.*

server.jsp.registered

server.servlet.jsp.registered

server.servlet-path

server.servlet.path

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-mustachespring-boot-starter-freemarker and spring-boot-starter-thymeleaf are not depending on it anymore. It is the developer’s responsibility to choose and add spring-boot-starter-web or spring-boot-starter-webflux.

已有 1 人翻译此段
我来翻译

Template engines

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.

A new 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-datatype-jdk8jackson-datatype-jsr310 and jackson-module-parameter-names. If you were manually depending on those modules, you can now depend on this new starter instead.

已有 1 人翻译此段
我来翻译

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.

Servlet filters

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 FilterRegistrationBean.

Note

Spring Security and Spring Session filters are configured for ASYNCERROR, and REQUEST dispatcher types.
已有 1 人翻译此段
我来翻译

RestTemplateBuilder

The 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.

WebJars Locator

Spring Boot 1.x used and provided dependency management for org.webjars:webjars-locatorwebjars-locator is a "poorly named library … that wraps the webjars-locator-core project". Dependencies on org.webjars:webjars-locator should be updated to use org.webjars:webjars-locator-core instead.

已有 1 人翻译此段
我来翻译
本文中的所有译文仅用于学习和交流目的,转载请务必注明文章译者、出处、和本文链接。
我们的翻译工作遵照 CC 协议,如果我们的工作有侵犯到您的权益,请及时联系我们。
加载中

评论(6)

slliver
slliver
谢谢~
zhisheng_t
zhisheng_t
http://www.54tianzhisheng.cn/2018/03/06/SpringBoot2-Migration-Guide/

我也参考官方文档翻译了下
水牛爱生活
感谢大神们的辛勤劳动,致敬!
宋晶磊
辛苦了
辣鸡程序员
辣鸡程序员
辛苦
axiba88
axiba88
辛苦了,各位😄
返回顶部
顶部