Upgrade spring boot from 1.5 series to 2.0 series

Introduction

Since 2 series of spring boot was released, it is a memo when upgrading because it is a big deal

The migration guide is officially available, so follow this procedure. Spring Boot 2.0 Migration Guide Also, this article was very helpful. Memorandum of understanding when Spring Boot 1.5.10 → Spring Boot 2.0.0

Correspondence contents

Introduction of property check tool

build.gradle as described in [Before You Start](https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#before-you-start) Add the following to .

build.gradle


runtime("org.springframework.boot:spring-boot-properties-migrator")

This will warn you of changes in the specifications of ʻapplication.yml`.

gradle plugin related fixes

Add plugin to resolve dependencies

Dependency Management plugin as described in [https://github.com/spring-projects/spring-boot/wiki/Spring-Boot-2.0-Migration-Guide#dependency-management) To add.

build.gradle


    apply plugin: 'org.springframework.boot'
    apply plugin: 'io.spring.dependency-management'  // <-- add this to your build.gradle

Fixed application.yml with changed syntax

Since spring.batch.initializer.enabled was changed to spring.batch.initialize-schema, change it here. Since it does not initialize, set never. Initialize a Spring Batch Database

application.yml


spring:
  batch:
    initializer:
       enabled: false

application.yml


spring:
  batch:
    initialize-schema: never

Camel case abolished on application.yml

What I set in camel case on application.yml was an error. Even if it's a kebab case. So I will make it a kebab case.

application.yml


spring:
  datasource:
    hogeHoge:
      driverClassName: com.mysql.jdbc.Driver

application.yml


spring:
  datasource:
    hoge-hoge:
      driver-class-name: com.mysql.jdbc.Driver

Build.gradle syntax change

Setting change when jar is generated

Previously, jar files were generated with bootRepackage for package generation, but this seems to be abolished. Replaced by bootJar and bootWar. However, due to the setting of the matter, bootJar is set to ʻenabled = false`.

build.gradle


    mainClassName = 'jp.co.hoge.fuga.App'
    bootRepackage {
        executable = true
    }

    jar {
      manifest {
          attributes 'GitHub-Branch-Name' : branchname
          attributes 'GitHub-Commit-Hash' : commithash
      }
    }

build.gradle


    ext.mainClass = 'jp.co.hoge.fuga.App'
    bootJar {
        enabled = false
    }

    jar {
      enabled = true
      manifest {
          attributes 'GitHub-Branch-Name' : branchname
          attributes 'GitHub-Commit-Hash' : commithash
      }
    }

gradlew version upgrade

Originally I used gradlew was 4.6, but I will give it to the latest 4.7.

build.gradle


task wrapper(type: Wrapper) {
    gradleVersion = "4.7"
}

Changes around the connection pool

Hikari CP compatible

I used to use tomcat connection pool before HikariCP has become the default, so change it there.

DataSourceConfiguration.java


@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.hoge")
public class DataSourceConfiguration {
    private String driverClassName;
    private String url;
    private String username;
    private String password;
    private Boolean testOnBorrow;
    private String validationQuery;

    public DataSource dataSource() {
      DataSource ds = new org.apache.tomcat.jdbc.pool.DataSource();
      ds.setDriverClassName(driverClassName);
      ds.setUrl(url);
      ds.setUsername(username);
      ds.setPassword(password);
      ds.setTestOnBorrow(testOnBorrow);
      ds.setValidationQuery(validationQuery);
      
      return ds;
    }
}

DataSourceConfiguration.java


@Data
@Component
@ConfigurationProperties(prefix = "spring.datasource.hoge")
public class DataSourceConfiguration {
    private String driverClassName;
    private String url;
    private String username;
    private String password;
    private Integer maxPoolSize;
    private String validationQuery;

    public HikariDataSource dataSource() {
      HikariDataSource ds = new HikariDataSource();
      ds.setDriverClassName(driverClassName);
      ds.setJdbcUrl(url);
      ds.setUsername(username);
      ds.setPassword(password);
      ds.setConnectionInitSql(validationQuery);
      ds.setMaximumPoolSize(maxPoolSize);
      
      return ds;
    }
}

You should also set query timeout etc, but I didn't set it this time because there is sql from batch that keeps running for a long time.

Deprecated classes and methods

Spring Mvc JavaConfig update

Due to spring5, WebMvcConfigurerAdapter has been deprecated. Since it says that you can use WebMvcConfigurer, change it there. I think it's because I started using the default method.

WebMvcConfig.java


@Configuration
public class WebMvcConfig extends WebMvcConfigurerAdapter {
       //Abbreviation
}

WebMvcConfig.java


@Configuration
public class WebMvcConfig implements WebMvcConfigurer {
       //Abbreviation
}

Abolition of Md5PasswordEncoder

Originally deprecated, it has been deprecated and needs to be changed.

PasswordEncoderConfig.java


@Bean
public PasswordEncoder md5PasswordEncoder() {
    return new Md5PasswordEncoder();
}

PasswordEncoderConfig.java


@Bean
public PasswordEncoder md5PasswordEncoder() {
    return new MessageDigestPasswordEncoder("MD5");
}

I would like to stop using MD5 itself, but it is not recommended because it is quite difficult for long-lasting projects, but I will avoid it once.

Using DelegatingPasswordEncoder

I will use it because it was added from spring security 5. It seems that it delegates the processing to the appropriate PasswordEncoder for each password hashing algorithm. There was a class that sets it by default, so use PasswordEncoderFactories to generate it. By default it returns bcrypt.

PasswordEncoderConfig.java


@Bean
public PasswordEncoder delegatingPasswordEncoder() {
    return PasswordEncoderFactories.createDelegatingPasswordEncoder();
}

Changes to org.hibernate.validator.constraints.NotBlank

ʻOrg.hibernate.validator.constraints.NotBlankhas been deprecated and will be changed. Sincejavax.validation.constraints.NotBlank` is implemented with the same class name, it was OK to replace only the import part at once.

With this kind of response, it works fine. end

Recommended Posts

Upgrade spring boot from 1.5 series to 2.0 series
Try Spring Boot from 0 to 100.
The story of raising Spring Boot from 1.5 series to 2.1 series part2
Story when moving from Spring Boot 1.5 to 2.1
Changes when migrating from Spring Boot 1.5 to Spring Boot 2.0
Changes when migrating from Spring Boot 2.0 to Spring Boot 2.2
What I did in the migration from Spring Boot 1.4 series to 2.0 series
What I did in the migration from Spring Boot 1.5 series to 2.0 series
The story of raising Spring Boot 1.5 series to 2.1 series
Introduction to Spring Boot ① ~ DI ~
Introduction to Spring Boot ② ~ AOP ~
Introduction to Spring Boot Part 1
Ubuntu Desktop upgrade from 18.0.4 (?) To 20.04.1 (focal)
How to set Spring Boot + PostgreSQL
Upgrade from MYSQL5.7 to 8.0 on CentOS 6.7
How to use ModelMapper (Spring boot)
Spring Boot starting from zero Part 2
Spring Boot starting from zero Part 1
02. I made an API to connect to MySQL (MyBatis) from Spring Boot
Change Spring Boot REST API request / response from CamelCase to SankeCase
Transition from Struts2 to Spring MVC (Controller)
How to split Spring Boot message file
Add spring boot and gradle to eclipse
Challenge Spring Boot
Spring Boot Form
Spring Boot Memorandum
gae + spring boot
Use Thymeleaf text template mode from Spring Boot
How to use built-in h2db with spring boot
How to make Spring Boot Docker Image smaller
How to use Spring Boot session attributes (@SessionAttributes)
Try to implement login function with Spring Boot
How to add a classpath in Spring Boot
Touch all Spring "Guides" (updated from time to time)
How to bind to property file in Spring Boot
Try to automate migration with Spring Boot Flyway
[Java] Article to add validation with Spring Boot 2.3.1.
I wanted to gradle spring boot with multi-project
Apply Twitter Bootstrap 4 to Spring Boot 2 using Webjars
◆ Get API created by Spring Boot from React
[Spring Boot] How to refer to the property file
[Introduction to Spring Boot] Authentication function with Spring Security
Spring Boot --How to set session timeout time
03. I sent a request from Spring Boot to the zip code search API
[Spring Boot] How to get properties dynamically from a string contained in a URL
From creating a Spring Boot project to running an application with VS Code
Plans to support JDK 11 for Eclipse and Spring Boot
SPRING BOOT learning record 01
How to set Dependency Injection (DI) for Spring Boot
Changes from Java 8 to Java 11
Spring Boot + Heroku Postgres
Sum from Java_1 to 100
1. Start Spring framework from 1
Migrate from JUnit 4 to JUnit 5
How to write a unit test for Spring Boot 2
Automatically map DTOs to entities with Spring Boot API
If you want to separate Spring Boot + Thymeleaf processing
Spring boot memo writing (1)
05. I tried to stub the source of Spring Boot
To connect from Spring to MySQL on virtual server (unsolved)
How to create a Spring Boot project in IntelliJ