从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) -> getOne(id)
  • 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部分。

  • 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

更多需要调整的参数请看文末参考文档。

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();
    }

未完待续...

参考文档

Spring Boot 2.0 Migration Guide
Spring Boot 2.0 Configuration Changelog
Spring Boot 2.0 新特性和发展方向

猜你喜欢

转载自blog.51cto.com/7308310/2133163