SpringBoot1.x升级到2.x遇到的坑总结
程序员文章站
2022-04-30 19:37:36
...
从springBoot1.x到springBoot2.x,有很多内容变化,其中springBoot2.x支持spring5以及webflux,这是一些比较新的东西,但是我在项目升级的过程中,发现很多配置以及方法与原来不一样,接下来总结下。
首先:JPA的不同。
这里主要涉及Repository接口几个方法修改:
- save --> saveAll
- findOne--> findById
- delete -->deleteById
其次:application配置文件修改
- server.context-path -->server.servlet.context-path
- spring.http --spring.servlet
- thymeleaf模式LEGAYHTML5 --> html
最后:还有在gradle-plugin中的一些问题
1.x之前的插件是有bootRepackage,现在是通过bootJar,并且配置bootJar来将boot打好。
1.x中总的build.gradle如下
allprojects{
group 'com.zhm'
version '1.0'
buildDir='target'
}
apply plugin: "idea"
subprojects{
configurations{
repositories{
maven { url "http://repo1.maven.org/maven2" }
maven { url "https://plugins.gradle.org/m2/" }
}
}
idea{
module{
excludeDirs=[file(".gradle")]
excludeDirs+=file("gradle")
excludeDirs+=file(".idea")
}
}
}
task run{
doFirst{
println 'run start'
}
doLast{
println 'run last'
}
}
run.dependsOn(':ui:runDev')
run.dependsOn(':server:bootRun')
task build{
doFirst{
println 'build start'
}
doLast{
println 'build last'
}
}
//当2.x时候就需要编程bootJar
build.dependsOn(':server:bootRepackage')
2.x中总的build.gradle
allprojects{
group 'com.zhm'
version '1.0'
buildDir='target'
}
apply plugin: "idea"
subprojects{
configurations{
repositories{
maven { url "http://repo1.maven.org/maven2" }
maven { url "https://plugins.gradle.org/m2/" }
}
}
idea{
module{
excludeDirs=[file(".gradle")]
excludeDirs+=file("gradle")
excludeDirs+=file(".idea")
}
}
}
task run{
doFirst{
println 'run start'
}
doLast{
println 'run last'
}
}
run.dependsOn(':ui:runDev')
run.dependsOn(':server:bootRun')
task build{
doFirst{
println 'build start'
}
doLast{
println 'build last'
}
}
build.dependsOn(':server:bootJar')
最后一行有变化。
1.x在server中的build.gradle
buildscript{
repositories {
mavenLocal()
//mavenCentral()
//jcenter()
maven { url "http://repo1.maven.org/maven2" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:1.5.13.RELEASE")
}
}
project(':server'){
apply plugin: 'org.springframework.boot'
apply from: "${rootDir}/gradle/java.gradle"
dependencies {
compile(project(":dao"))
runtime("com.alibaba:druid:1.1.9")
compile("com.alibaba:druid-spring-boot-starter:1.1.9")
compile("com.alibaba:fastjson:1.2.36")
compile("commons-codec:commons-codec:1.11")
compile("org.apache.commons:commons-collections4:4.1")
compile("commons-io:commons-io:2.6")
compile("org.apache.commons:commons-lang3:3.7")
compile("com.google.guava:guava:23.6-jre")
compile("com.squareup.okhttp3:okhttp:3.10.0")
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-cache')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-redis')
compile('org.springframework.boot:spring-boot-starter-log4j2')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-validation')
compile('org.springframework.boot:spring-boot-starter-mail:1.5.13.RELEASE')
compile("io.springfox:springfox-swagger2:2.9.2")
compile("io.springfox:springfox-swagger-ui:2.9.2")
compile("org.hibernate:hibernate-envers:5.2.17.Final")
compile("org.hibernate:hibernate-core:5.2.17.Final")
//compile("org.hibernate:hibernate-core:4.1.2.Final")
compile("org.hibernate:hibernate-ehcache:5.2.17.Final")
compile("org.antlr:antlr4:4.7.1")
runtime("mysql:mysql-connector-java")
compile("com.github.penggle:kaptcha:2.3.2")
compile("org.apache.poi:poi:$poi_version")
compile("org.apache.poi:poi-ooxml:$poi_version")
compile("org.apache.pdfbox:pdfbox:$pdfbox_version")
compile("org.apache.pdfbox:pdfbox-tools:$pdfbox_version")
compile group: 'org.apache.velocity', name: 'velocity', version: '1.7'
compile group: 'commons-configuration', name: 'commons-configuration', version: '1.10'
compile group: 'com.auth0', name: 'java-jwt', version: '3.3.0'
compile group: 'org.apache.commons', name: 'commons-text', version: '1.8'
compile group: 'org.lionsoul', name: 'ip2region', version: '1.7.2'
// https://mvnrepository.com/artifact/jakarta.jms/jakarta.jms-api
}
jar {
destinationDir = file("${rootProject.buildDir}")
archiveName = "${rootProject.name}.jar"
if(file("${rootDir}/ui/dist").exists()) {
from("${rootDir}/ui/dist") {
into 'META-INF/resources'
}
}
}
bootRun.systemProperty 'spring.profiles.active', 'development'
springBoot {
backupSource = false
executable = false
}
}
在2.x中server中的build.gradle
buildscript{
repositories {
mavenLocal()
maven { url "http://repo1.maven.org/maven2" }
maven { url "https://plugins.gradle.org/m2/" }
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.2.0.RELEASE")
}
}
project(':server'){
apply plugin: 'java'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
apply from: "${rootDir}/gradle/java.gradle"
dependencies {
compile(project(":dao"))
compile('org.springframework.boot:spring-boot-starter')
compile('org.springframework.boot:spring-boot-starter-cache')
compile('org.springframework.boot:spring-boot-starter-data-jpa')
compile('org.springframework.boot:spring-boot-starter-data-redis')
compile('org.springframework.boot:spring-boot-starter-log4j2')
compile('org.springframework.boot:spring-boot-starter-web')
compile('org.springframework.boot:spring-boot-starter-validation')
compile('org.springframework.boot:spring-boot-starter-mail')
compile('org.springframework.boot:spring-boot-starter-actuator')
// https://mvnrepository.com/artifact/mysql/mysql-connector-java
compile group: 'mysql', name: 'mysql-connector-java', version: '5.1.46'
//验证码jar包
compile("com.github.penggle:kaptcha:2.3.2")
//java web token
compile group: 'com.auth0', name: 'java-jwt', version: '3.3.0'
//导出Excel
compile("org.apache.poi:poi:$poi_version")
compile("org.apache.poi:poi-ooxml:$poi_version")
//读取pdf
compile("org.apache.pdfbox:pdfbox:$pdfbox_version")
compile("org.apache.pdfbox:pdfbox-tools:$pdfbox_version")
compile("org.apache.pdfbox:fontbox:$pdfbox_version")
//httpClient
compile("com.squareup.okhttp3:okhttp:$okhttp_version")
compile("org.apache.httpcomponents:httpclient:$httpclient_version")
//获取系统信息
compile("com.github.oshi:oshi-core:$oshi_version")
}
//1.x中是jar,2.x改成bootJar
bootJar{
destinationDir = file("${rootProject.buildDir}")
archiveName = "${rootProject.name}.jar"
if(file("${rootDir}/ui/dist").exists()) {
from("${rootDir}/ui/dist") {
into 'META-INF/resources'
}
}
}
bootRun.systemProperty 'spring.profiles.active', 'development'
//1.x与2.x是完全不一样的。
springBoot {
mainClassName='com.zhm.DemogApplication'
buildInfo()
}
}
推荐阅读