标签: Spring Boot
本文档主要通过开发指南的方式来帮助您将应用程序迁移到 Spring Boot 2.0。
在你开始之前
首先,Spring Boot 2.0.0 要求 Java 8 或更高版本,不再支持 Java 6 和 7。
在 Spring Boot 2.0 中,许多配置属性已被重命名或被删除,相应地,开发者需要升级他们的 application.properties
/application.yml
。为了方便升级,Spring Boot 发布了一个新的 spring-boot-properties-migrator
模块。只要将其作为依赖添加到项目中,它不仅会分析应用程序的环境并在启动时打印诊断信息,而且还会在运行时为项目临时迁移属性。在您的应用程序迁移期间,这个模块是必备的:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-properties-migrator</artifactId>
</dependency>
注意 | 迁移完成之后,请确保从项目的依赖关系中移除该模块。 |
如果你想查看具体内容,请参阅以下资源,或者继续阅读下一节
构建你的 Spring Boot 应用
Spring Boot 的 Maven 插件
为保持一致性,以及避免与其它插件的冲突,现在暴露的插件配置属性都以 spring-boot
为前缀。
例如,使用下面的命令行能启用名字为 foo 的配置文件:
mvn spring-boot:run -Dspring-boot.run.profiles=foo
Surefire 插件的默认值
惯用的 include/exclude 配置模式已经和最新的 Surefire 插件默认集成。如果依赖于此插件,需要相应地更新插件配置。之前对应的配置如下:
<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>
注意:如果你使用的是 JUnit 5 版本,你应该将 Surefire 降级到 2.19.1 版本。
**/*Tests.java
模式不包含在此版本,所以如果你依赖于它,请确保在你已配置文件中将其添加。
Spring Boot 的 Gradle 插件
Spring Boot 的 Gradle 插件经过了重大的重写,并带来了大量的改进。你可以通过参阅手册和 api 文档以了解插件的功能。
依赖管理
Spring Boot 的 Gradle 插件不再自动应用依赖管理插件。与之代替的是,Spring Boot 的插件现在可以通过导入正确版本的 spring-boot-dependencies bom 来应用依赖管理插件。当依赖管理被配置的时候,这一点会让你有更多的控制权。
对于大多数应用程序,使用应用依赖管理插件就足够了:
apply plugin: 'org.springframework.boot' apply plugin: 'io.spring.dependency-management' // <-- 添加这个到你的 build.gradle
注意 | 依赖管理插件仍然是 spring-boot-gradle-plugin 的传递依赖项,所以不需要在 buildscript 配置中将其列为类路径依赖项。 |
构建可执行的 jar 和 war
bootRepackage 任务已经被替换成 bootJar 和 bootWar 任务,分别用于构建可执行的 jar 包和 war 包。
Spring Boot 特性
默认动态代理策略
Spring Boot现在默认使用CGLIB动态代理(基于类的动态代理), 包括AOP. 如果需要基于接口的动态代理(JDK基于接口的动态代理) , 需要设置spring.aop.proxy-target-class属性为false.
SpringApplication
Web Environment
Spring Boot 应用现状可以运行于更多的模式, 所以spring.main.web-environment属性现在不赞成使用, 可以使用spring.main.web-application-type提供更多功能.
如果想让应用以非web服务方式启动, 需要更改属性值:
spring.main.web-application-type=none
Tip | 可以通过SpringApplication的setWebApplicationType方法实现 |
Spring Boot 应用时间变更
我们提供了一个新的事件(event)-ApplicationStartedEvent. ApplicationStartedEvent在context刷新之后, 应用或命令行调用之前发送(send). ApplicationReadyEvent在应用活命令行调用之后发送. 表名应用以准备好提供服务.
参考 updated reference documentation.
Banner
我们想要减少Spring Boot使用的命名空间数量, banner-related属性迁移到spring.banner.
外部化配置
Relaxed binding 宽松绑定
有关宽松绑定的规则已经收紧。 我们假设有一个的acme.my-project.my-name属性:
所有前缀必须是kebab格式(小写,连字符分隔),acme.myProject或acme.my_project是无效的 - 你必须在此处使用acme.my-project
属性名称可以使用kebab-case(my-name),camel-case(myName)或snake-case(my_name)
环境属性(来自OS环境变量)必须使用常规的大写下划线格式,其中下划线只能用于分隔关键词的各个部分,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 theprefix
attribute you can now simply put the full key using thename
orvalue
attributesRelaxedPropertyResolver
is no longer available as theEnvironment
takes care of that automatically:env.getProperty("com.foo.my-bar")
will find acom.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.
开发网页应用
嵌入式容器封装结构
为了支持响应式的使用案例,嵌入式容器封装结构已经很大程度上进行重构。EmbeddedServletContainer已经被重命名为WebServer, 同时org.springframework.boot.context.embedded包已经被迁移至org.springframework.boot.web.embedded。例如,如果你之前使用TomcatEmbeddedServletContainerFactory回调接口来自定义嵌入式的Tomcat容器,现在你应该使用TomcatServletWebServerFactory。
Servlet-specific的关于server的属性
一些Servlet-specific已经移动到server.servlet的server.*属性:
旧属性 | 新属性 |
---|---|
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
先前一些Spring Boot Starter是依赖于Spring MVC的 spring-boot-starter-web。在Spring WebFlux的新的支持下,spring-boot-starter-mustache, spring-boot-starter-freemarker 和spring-boot-starter-thymeleaf不再依赖它了。选择并添加spring-boot-starter-web 或spring-boot-starter-webflux是开发者的职责。
模板引擎
Mustache模板默认的文件扩展名
Mustache模板的文件扩展名曾经是.html。现在的扩展名为.mustache,与官方规格和大多数的IDE插件保持一致。你可以通过更改spring.mustache.suffix配置文件的configuration key来重写这个新的默认值。
Jackson/JSON支持
在2.0版本,我们已经设置Jackson配置文件的默认值,将ISO-8601 strings写作了JSR-310。如果你希望返回之前的设置,可以添加spring.jackson.serialization.write-dates-as-timestamps=true 到你的配置文件中。
一个新的spring-boot-starter-json starter收集了必要的位去读写JSON。它不仅提供了jackson-databind,而且提供了和Java8一起运作的时候相当有用的组件:jackson-datatype-jdk8, jackson-datatype-jsr310 和jackson-module-parameter-names。如果你曾经手动地依赖这些组件,现在可以依赖这个新的starter取代。
符合默认行为变化的Spring MVC路径
我们已经决定更改默认关于Spring MVC应用的后缀路径(看#11105)。这个性质已经不再默认启动,请参照best practice documented in Spring Framework。
如果你的应用期望像 "GET /projects/spring-boot.json" 这样的请求可以被映射到@GetMapping("/projects/spring-boot"),这一变化正在影响你。
有关的更多信息和如何减少这种变化,请参考 Spring Boot有关路径匹配和内容协商的参考文档。
Servlet过滤器
现在Servlet过滤器默认的dispatcher控制器类型是DipatcherType.REQUEST;这个Spring Boot的默认值与Servlet specification的默认值保持一致。如果你想要映射一个过滤器到其它的控制器类型,请使用FilterRegistrationBean注册你的过滤器。
注意:Spring Security和Spring Session过滤器是用来配置 ASYNC, ERROR, 和REQUEST的控制器类型。
RestTemplateBuilder
requestFactory(ClientHttpRequestFactory) 被替换成一个新的requestFactory(Supplier<ClientHttpRequestFactory> requestFactorySupplier). Supplier接口允许每个template使用自己的request factory创建, 从而避免了共享一个工厂方法的不良影响. 参考 #11255.
WebJars Locator
Spring Boot 1.x 提供的org.webjars:webjars-locator. webjars-locator依赖管理是 "poorly named library … that wraps the webjars-locator-core project". org.webjars:webjars-locator依赖应替换成org.webjars:webjars-locator-core.