SpringBoot - Upgrade (v1.5.10.RELEASE -- v2.2.6.RELEASE)
springboot 2.x版本已经发行很长时间了,但我们项目依旧使用的时1.x的版本,最近手头上的工作略微少了一点,趁此机会想着升级一把springboot的版本。spring的官网(https://start.spring.io/)目前最新稳定版已经是2.2.6了,而且添加了不少新特性呢,来,搞起来;
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.6.RELEASE)
①把原来的springboot的版本依赖,从pom文件中拿掉,换成新版本的依赖信息;当然springboot2.2.6版本支持的JDK版本已经要求最低1.8了,别忘记了哦~,还有一些依赖的版本要注释掉,比如netty jackson ; 有一些依赖的版本要换成新版的,比如fastjson(1.2.46 --> 1.2.58)
<springboot.version>2.2.6.RELEASE</springboot.version>
<java.version>1.8</java.version>
<!--<netty.version>4.0.33.Final</netty.version>-->
<!--<jackson.version>2.7.2</jackson.version>-->
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.6.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
<!--<version>4.5.2</version>-->
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<!--<version>1.2.46</version>-->
<version>1.2.58</version>
</dependency>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-jpa</artifactId>
<!--<version>1.11.12.RELEASE</version>-->
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<!--<version>5.1.42</version>-->
</dependency>
<dependency>
<groupId>io.netty</groupId>
<artifactId>netty-all</artifactId>
<!--<version>4.0.33.Final</version>-->
</dependency>
②我们的项目使用了Cassandra的数据库,配置了Cassandra的连接信息,要修改一些地方;
默认开启jmxreport会报错:https://stackoverflow.com/questions/53101753/spring-boot-data-cassandra-reactive-jmxreporter-problem
dbConf:
old: import org.springframework.data.cassandra.config.java.AbstractCassandraConfiguration;
new: import org.springframework.data.cassandra.config.AbstractCassandraConfiguration;
dbConn:
CassandraClusterFactoryBean cluster = new CassandraClusterFactoryBean();
cluster.setJmxReportingEnabled(false); //默认是开启Jmxreport,我们需要关闭它
dbModel:
old: import org.springframework.data.cassandra.mapping.Table;
new: import org.springframework.data.cassandra.core.mapping.*;
③项目中以前的findOne方法和delete方法在springboot2.x版本中均已被移除;
方法调用的改变:
使用deleteById(Long id)来替换,还有一个不同点是deleteById(Long id)默认实现返回值为void;
deviceRepo.findOne(devEUI); ---> deviceRepo.findBySN(devEUI); //这里是自己实现的自定义 Sql方法 或者 使用 findById方法;并使用isPresent做非空判断
//return deviceRepo.findOne(devEUI);
//return deviceRepo.findBySN(devEUI);
if (deviceRepo.findById(devEUI).isPresent()) {
return deviceRepo.findById(devEUI).get();
}
return null;
deviceRepo.delete(endpoint); ---> deviceRepo.deleteById(endpoint);
deviceRepo.exists(uplink.getSerialNumber()) ---> deviceRepo.existsById(uplink.getSerialNumber())
deviceRepo.findOne(device.getEndpointName()) ---> deviceRepo.getOne(uplink.getSerialNumber()) //这里Oracle是使用getOne方法或findById方法均可
④配置文件application.yml也有一些地方需要修改,因为2.x版本的连接池由tomcat-pool换为HikariPool,连接配置要修改;
##这里是oracle或者mysql连接配置的修改;
url: ---> jdbc-url:
##这里应该是springboot以后要支持更多的servlet配置,以数组的形式配置,但是不修改也不影响
context-path: / ---> servlet:
context-path: /
##logback的配置有所变化,但是不修改也不影响
logging:
config: classpath:logback.xml
⑤有oracle配置的话,find方法可能要注意命名规范的问题,在2.x版本中对此的检查更加严格;
Caused by: org.springframework.data.mapping.PropertyReferenceException: No property SN found for type Device!
Caused by: org.hibernate.hql.internal.ast.QuerySyntaxException: unexpected token: * near line 1, column 8 [SELECT * FROM com.company.model.Device WHERE serial_num = :serial_num]
//数据库字段为serial_num,参数名为serialNum
@Id
@Column(name = "serial_num", unique = true, nullable = false)
private String serialNum;
//自定义的sql方法,必须是跟参数名一致,否则就会报错
@Query("SELECT * FROM device WHERE serial_num = :serial_num")
Device findBySerialNum(@Param("serial_num") String serial_num);
⑥oracle配置: 下面的报错只需要将原来的property name 'oracleDS' 改为 ‘oracle’即可,原因下面的报错信息也有提示的。
Description:
Configuration property name 'oracleDS' is not valid:
Invalid characters: 'D', 'S'
Bean: entityManagerFactoryPrimary
Reason: Canonical names should be kebab-case ('-' separated), lowercase alpha-numeric characters and must start with a letter
Action:
Modify 'oracleDS' so that it conforms to the canonical names requirements.
@Bean(name = "primaryDataSource")
@Primary
@Qualifier("primaryDataSource")
@ConfigurationProperties(prefix="oracle") //原来是 @ConfigurationProperties(prefix="oracleDS")
public DataSource primaryDataSource() {
return DataSourceBuilder.create().build();
}
至此,愉快的打包运行测试即可啦。。。 。。。
2020-04-30 13:25:15.480 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Starting...
2020-04-30 13:25:15.805 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Start completed.
... ...
2020-04-30 13:32:12.917 [main] INFO o.s.orm.jpa.LocalContainerEntityManagerFactoryBean - Closing JPA EntityManagerFactory for persistence unit 'primaryPersistenceUnit'
2020-04-30 13:32:12.918 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown initiated...
2020-04-30 13:32:12.940 [main] INFO com.zaxxer.hikari.HikariDataSource - HikariPool-1 - Shutdown completed.