从Spring Boot 1.5升级到2.0
POM
- 1.5
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.10.RELEASE</version> <relativePath/> </parent>
- 2.0
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.0.3.RELEASE</version> <relativePath/> </parent>
Spring Data
Spring Data的一些方法进行了重命名:
- findOne(id) -> findById(id) (返回的是Optional)
- delete(id) -> deleteById(id)
- exists(id) -> existsById(id)
- findAll(ids) -> findAllById(ids)
配置
在升级时,可以在pom中增加spring-boot-properties-migrator,这样在启动时会提示需要更改的配置:
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-properties-migrator</artifactId> <scope>runtime</scope> </dependency>
其中改动较大的是security(The security auto-configuration is no longer customizable,A global security auto-configuration is now provided)、management、banner、server等。
- security
security开头的配置及management.security均已过期,如以下的配置不再支持,需要调整到代码中:security: ignored: /api-docs,/swagger-resources/**,/swagger-ui.html**,/webjars/**
- management
management: security: enabled: false port: 8090
修改为:
management: server: port: 8090
- datasource
datasource: initialize: false
修改为:
datasource: initialization-mode: never
如使用PostgreSQL,可能会报错:Method org.postgresql.jdbc.PgConnection.createClob() is not yet implemented hibernate,需修改配置:
jpa: database-platform: org.hibernate.dialect.PostgreSQLDialect properties: hibernate: default_schema: test jdbc: lob: non_contextual_creation: true
- banner调整为spring.banner
- server调整为server.servlet
更多需要调整的参数请看文末参考文档。
Security
WebSecurityConfigurerAdapter
- security.ignored
@Override public void configure(WebSecurity web) throws Exception { web.ignoring().antMatchers("/api-docs", "/swagger-resources/**", "/swagger-ui.html**", "/webjars/**"); }
- AuthenticationManager
如在代码中注入了AuthenticationManager,@Autowired private AuthenticationManager authenticationManager;
在启动时会报错:Field authenticationManager required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.请在WebSecurityConfigurerAdapter中增加以下代码:
@Bean @Override public AuthenticationManager authenticationManagerBean() throws Exception { return super.authenticationManagerBean(); }
Actuator
- 配置属性变化
Old property | New property |
---|---|
endpoints.id.* | management.endpoint.id.* |
endpoints.cors.* | management.endpoints.web.cors.* |
endpoints.jmx.* | management.endpoints.jmx.* |
management.address | management.server.address |
management.context-path | management.server.servlet.context 5b4 -path |
management.ssl.* | management.server.ssl.* |
management.port | management.server.port |
management.endpoints.web.base-path的默认值为/actuator,即Actuator访问路径前部增加了actuator([/actuator/health],[/actuator/info]),这可以在启动日志中看到。
因management.security不再支持,权限配置需添加到WebSecurityConfigurerAdapter中:
.authorizeRequests() .requestMatchers(EndpointRequest.to("health", "info")).permitAll()
- Endpoint变化
Endpoint | Changes |
---|---|
/actuator | No longer available. There is, however, a mapping available at the root of management.endpoints.web.base-path that provides links to all the exposed endpoints. |
/auditevents | The after parameter is no longer required |
/autoconfig | Renamed to /conditions |
/docs | No longer available (the API documentation is part of the published documentation now) |
/health | Rather than relying on the sensitive flag to figure out if the health endpoint had to show full details or not, there is now a management.endpoint.health.show-details option: never, always, when-authorized. By default, /actuator/health is exposed and details are not shown. 5b28 |
/trace | Renamed to /httptrace |
新特性
-
Configuration Property Binding
可以在代码中直接使用Binder API从配置文件中读取内容:public class Person implements EnvironmentAware { private Environment environment; @Override public void setEnvironment(Environment environment) { this.environment = environment; } public void bind() { List<PersonName> people = Binder.get(environment) .bind("my.property", Bindable.listOf(PersonName.class)) .orElseThrow(IllegalStateException::new); } }
YAML配置
my: property: - first-name: Jane last-name: Doe - first-name: John last-name: Doe
- Spring Data Web Configuration
新增spring.data.web配置来设置分页和排序:# DATA WEB (SpringDataWebProperties) spring.data.web.pageable.default-page-size=20 # Default page size. spring.data.web.pageable.max-page-size=2000 # Maximum page size to be accepted. spring.data.web.pageable.one-indexed-parameters=false # Whether to expose and assume 1-based page number indexes. spring.data.web.pageable.page-parameter=page # Page index parameter name. spring.data.web.pageable.prefix= # General prefix to be prepended to the page number and page size parameters. spring.data.web.pageable.qualifier-delimiter=_ # Delimiter to be used between the qualifier and the actual page number and size properties. spring.data.web.pageable.size-parameter=size # Page size parameter name. spring.data.web.sort.sort-parameter=sort # Sort parameter name.
- 支持自定义JdbcTemplate
# JDBC (JdbcProperties) spring.jdbc.template.fetch-size=-1 # Number of rows that should be fetched from the database when more rows are needed. spring.jdbc.template.max-rows=-1 # Maximum number of rows. spring.jdbc.template.query-timeout= # Query timeout. Default is to use the JDBC driver's default configuration. If a duration suffix is not specified, seconds will be used.
- 支持hibernate自定义命名策略
- Reactive
更多新特性请查看Release Notes。
参考文档
Spring Boot Reference Guide 2.0.3.RELEASE
Spring Boot 2.0 Migration Guide
Spring Boot 2.0 Configuration Changelog
Spring Boot 2.0 Release Notes
Spring Boot Relaxed Binding 2.0
- 升级到SpringBoot2.0后 properties文件 中文乱码问题
- spring cloud项目升级spring boot 2.0爬过的坑
- 给大家聊一聊云收藏从 Spring Boot 1.0 升级到 2.0 所踩的坑
- spring boot 1.x升级到spring boot 2.0
- Springboot2.0 升级(Gradle工程)
- 解决Spring Boot OTS parsing error: Failed to convert WOFF 2.0
- 49. spring boot日志升级篇—理论【从零开始学Spring Boot】
- Spring Boot 2.0(四):使用 Docker 部署 Spring Boot
- 基于Spring Boot 2.0 及MongoDB 3.6.2 实现的简单文件共享服务器
- Spring2.5.6 OSGi升级Spring3.0,springDM2.0报错:
- Java 9 和Spring Boot 2.0纷纷宣布支持的HTTP/2到底是什么?
- 阿里P9告诉你 Spring Boot 2.0正式发布,升还是不升呢?
- Spring boot 2.0 新特性之动态 Banner
- Spring boot2.0 启动报错 java.lang.ClassNotFoundException: javax.xml.bind.JAXBException
- 49. spring boot日志升级篇—理论【从零开始学Spring Boot】(
- SpringBoot2.0如何启用https协议
- Spring Boot 2.0新增的Actuator端点的特性
- 慕课网Spring Boot 2.0深度实践-初遇Spring Boot 笔记
- Spring boot for Eclipse 开发指南第六节 Oauth 2.0
- spring boot2.0配置log4j2 发布到生成环境 报内存溢出异常 解决方案