千家信息网

从Spring Boot 1.5升级到2.0

发表于:2026-11-01 作者:千家信息网编辑
千家信息网最后更新 2026年11月01日,POM1.5 org.springframework.boot spring-boot-starter-parent 1.5.10.RELEASE 2.0 org.spr
千家信息网最后更新 2026年11月01日从Spring Boot 1.5升级到2.0

POM

  • 1.5
    org.springframework.boot    spring-boot-starter-parent    1.5.10.RELEASE    
  • 2.0
    org.springframework.boot    spring-boot-starter-parent    2.0.7.RELEASE    

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,这样在启动时会提示需要更改的配置:

    org.springframework.boot    spring-boot-properties-migrator    runtime

其中改动较大的是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
@Overridepublic void configure(WebSecurity web) throws Exception {    web.ignoring().antMatchers("/api-docs", "/swagger-resources/**", "/swagger-ui.html**", "/webjars/**");}
  • AuthenticationManager

如在代码中注入了AuthenticationManager,

@Autowiredprivate AuthenticationManager authenticationManager;

在启动时会报错:Field authenticationManager required a bean of type 'org.springframework.security.authentication.AuthenticationManager' that could not be found.请在WebSecurityConfigurerAdapter中增加以下代码:

@Bean@Overridepublic AuthenticationManager authenticationManagerBean() throws Exception {    return super.authenticationManagerBean();}

Actuator

  • 配置属性变化
Old propertyNew property
endpoints.id.*management.endpoint.id.*
endpoints.cors.*management.endpoints.web.cors.*
endpoints.jmx.*management.endpoints.jmx.*
management.addressmanagement.server.address
management.context-pathmanagement.server.servlet.context-path
management.ssl.*management.server.ssl.*
management.portmanagement.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变化
EndpointChanges
/actuatorNo 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.
/auditeventsThe after parameter is no longer required
/autoconfigRenamed to /conditions
/docsNo longer available (the API documentation is part of the published documentation now)
/healthRather 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.
/traceRenamed 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 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。

Swagger

    io.springfox    springfox-swagger2    2.9.2    io.springfox    springfox-swagger-ui    2.9.2

Swagger 2.9版本,增加了对@ApiParam属性example的处理,在Swagger UI和文档中会显示,因此要注意设置合适的example值:

@ApiOperation(value = "Delete airline by id")@GetMapping("/airlines/delete/{airlineId}")public void deleteAirline(@ApiParam(required = true, example = "123") @PathVariable Long airlineId)

否则你会看到Warn日志:
AbstractSerializableParameter

@JsonProperty("x-example")public Object getExample() {    if (example == null) {        return null;    }    try {        if (BaseIntegerProperty.TYPE.equals(type)) {            return Long.valueOf(example);        } else if (DecimalProperty.TYPE.equals(type)) {            return Double.valueOf(example);        } else if (BooleanProperty.TYPE.equals(type)) {            if ("true".equalsIgnoreCase(example) || "false".equalsIgnoreCase(defaultValue)) {                return Boolean.valueOf(example);            }        }    } catch (NumberFormatException e) {        LOGGER.warn(String.format("Illegal DefaultValue %s for parameter type %s", defaultValue, type), e);    }    return example;}

另外Swagger 2.9.2会为org.springframework.data.domain.Pageable自动增加@ApiImplicitParams。

参考文档

Spring Boot Reference Guide 2.0.7.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

配置 代码 支持 调整 文档 属性 日志 更多 特性 会报 参考 变化 升级 合适 较大 内容 参数 开头 文件 方法 数据库的安全要保护哪些东西 数据库安全各自的含义是什么 生产安全数据库录入 数据库的安全性及管理 数据库安全策略包含哪些 海淀数据库安全审计系统 建立农村房屋安全信息数据库 易用的数据库客户端支持安全管理 连接数据库失败ssl安全错误 数据库的锁怎样保障安全 互联网佳科技有限公司 关于新基建和互联网科技 与互联网科技公司 互联网金融与金融科技 北京互联网科技有限公司 互联网医疗科技 互联网科技股有哪些 富国互联网科技创新基金 博兴的互联网电子科技公司 昊天互联网科技有限公司 米聚互联网科技南京有限公司排面 青海科技产业互联网发展 工业互联网等科技实现智能化赋能 为客找房互联网科技公司怎么样 上海创道互联网络科技有限公司 山东大美朱楼互联网科技有限公司 容联科技互联网电商 互联网科技企业商标注意事项 互联网金融金融科技 每日互联网科技新闻 苏州找大状互联网科技有限公司 A股互联网金融科技公司 互联网公司 科技公司 互联网金融 科技 农村开互联网科技讲堂 中国互联网有什么科技含量 互联网科技有哪些公司 湖北码尼互联网科技有限公司电话 千方科技属于互联网企业吗 杭州科技互联网公司有哪些
0