從Spring Boot 1.5升級到2.0

來源:互聯網
上載者:User

標籤:uid   htm   exist   info   參考   hue   could not   重新命名   tom   

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開頭的配置及management.security均已到期,如以下的配置不再支援,需要調整到代碼中:
    security:ignored: /api-docs,/swagger-resources/**,/swagger-ui.html**,/webjars/**
  • management
    management:security:enabled: falseport: 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
    @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

Actuator訪問路徑發生了變化,網址前部增加了單詞actuator([/actuator/health],[/actuator/info]),這可以在開機記錄中看到。

未完待續...

參考文檔

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

從Spring Boot 1.5升級到2.0

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.