SpringBoot1.5.x升级到2.X经验总结

最近因为有安全需求和其他方面的需求,我们项目的springboot整体要求升级到较高的版本。 因为我们以前版本是1.5.6.RELEASE
这次升级把目的版本号定到了

2.0.1.RELEASE

以下是一些经验总结,供大家参考
针对配置文件的变更:
一、多数据源配置中数据库连接

url:
变更为
jdbc-url:

二、添加新的日志配置方式

application.yml
logging:
  config: classpath:logback-spring.xml

最后附logback-spring.xml文件内容

三、redis配置变更

pool.max-idle: 8 # pool settings ...
pool.min-idle: 1
pool.max-active: 8
pool.max-wait: -1

变更为
jedis:
       pool.max-idle: 8 # pool settings ...
       pool.min-idle: 1
       pool.max-active: 8
       pool.max-wait: -1

四、context-path放到了servlet下

server:
  tomcat:
    max-http-post-size: -1
  context-path: /
  port: 9999

变更为

server:
  tomcat:
    max-http-post-size: -1
  servlet:
    context-path: /
  port: 9999

五、multipart 相关放到了servlet。这表明以后springboot可能不只添加servlet一种方式,可能还会添加其他的容器

http:    
    multipart:
      max-file-size: 5MB
      max-request-size: 100MB
变更为
servlet:
    multipart:
      max-file-size: 5MB
      max-request-size: 100MB

六、mybatis的扫描

#mybatis.mapperLocations=classpath*:/mybatis/*/*Mapper.xml
修改为:
mybatis.mapper-locations=classpath*:/mybatis/*/*Mapper.xml

七、启用外部配置文件
–spring.config.location 变更为 --spring.config.additional-location

nohup java -jar service.caiyouxi.com-1.0-SNAPSHOT.war --spring.config.location=application.properties &
修改为
nohup java -jar service.caiyouxi.com-1.0-SNAPSHOT.war --spring.config.additional-location=application.properties &

八、content-type放到了servlet下面

#spring.thymeleaf.content-type=text/html
修改为
spring.thymeleaf.servlet.content-type=text/html

九、mybatis别名包

#mybatis.typeAliasesPackage=com.caiyouxiouxi
修改为
#mybatis别名包
mybatis.type-aliases-package=com.caiyouxi.order

十、WebAppConfig变更

spring2.x中即可以继承WebMvcConfigurationSupport 也可以实现WebMvcConfigurer。但是老的继承WebMvcConfigurerAdapter已经过时了

MyWebAppConfig extends WebMvcConfigurerAdapter

变更为

MyWebAppConfig extends WebMvcConfigurationSupport {
	/**
	 * springboot 2.X
	 * implements WebMvcConfigurer
	 * extends WebMvcConfigurationSupport
	 * 
	 * springboot 1.5.x
	 * extends WebMvcConfigurerAdapter
	 */

遇到的错误:
‘gsonBuilder’ defined in class path resource [org/springframework/boot/autoconfigure/gson/GsonAutoC

针对POM的变更:
一、指定编译版本和JAVA版本
因为springboot2.X针对JAVA版本最低要求为1.8
Spring Boot 2.0 requires Java 8 as a minimum version.

	
	
		UTF-8
		UTF-8
		
		UTF-8
		1.8
	

二、springboot对 webflux进行了支持,为了方便,我把
spring-boot-starter-web 和spring-boot-starter-webflux 都进行了引用

Web Starter as a Transitive Dependency
Previously several Spring Boot starters were transitively depending on Spring MVC with spring-boot-starter-web. With the new support of Spring WebFlux, spring-boot-starter-mustache, spring-boot-starter-freemarker and spring-boot-starter-thymeleaf are not depending on it anymore. It is the developer’s responsibility to choose and add spring-boot-starter-web or spring-boot-starter-webflux.

	
	
		org.springframework.boot
		spring-boot-starter-web
	
	
	
		org.springframework.boot
		spring-boot-starter-webflux
	

三、不再指定mysql-connector-java的版本

	
		mysql
		mysql-connector-java
		
		runtime
	

四、mybatis依赖升级为2.0.1

	
		org.mybatis.spring.boot
		mybatis-spring-boot-starter
		
		2.0.1
	

五、针对druid的依赖由1.1.3升级为1.1.10

	
		com.alibaba
		druid-spring-boot-starter
		1.1.10
	

六、fastjson 升级到1.2.58

	
		com.alibaba
		fastjson
		1.2.58
	

七、dom4j 和jaxen不再指定版本

	
		dom4j
		dom4j
		
	
	
		jaxen
		jaxen
		
	

八、httpcomponents包下的引用不再指定版本号

	
		org.apache.httpcomponents
		httpclient
		
	

	
	
		org.apache.httpcomponents
		httpmime
		
	

	
	
		org.apache.httpcomponents
		httpcore
		
	

九、定时任务的依赖变更,springboot2.x对 quartz有了支持

    原先的


        
            org.quartz-scheduler
            quartz
            1.8.5
        
        改为

		
			org.springframework.boot
			spring-boot-starter-quartz
		

十、okhttp3由原来的3.3.0升级至3.14.2

	
	
		com.squareup.okhttp3
		okhttp
		3.14.2
	

十一、如果用到了guava cache要排除对应的包,springboot2.x不再支持guava cache。
并且把GuavaConfig中的个性化配置注释掉.

GuavaCacheManager
Support for Guava has been dropped in Spring Framework 5. If you were are using GuavaCacheManager, Caffiene (com.github.ben-manes.caffeine:caffeine) and CaffeineCacheManager should be used instead.

 
       org.springframework.boot
       spring-boot-starter-cache
 
 
       com.google.guava
       guava
       19.0
 

十二、surefire-junit3升级到surefire-junit4


    org.apache.maven.surefire
    
    
    
    
    
    
    surefire-junit4
    2.22.2

附件:
logback-spring.xml








    

    
    
    
     
    
    
    
    
     
    


    ${APP_NAME}
    
    
    
    
    
    
    


    
    
        
        
            info
        
        
            ${CONSOLE_LOG_PATTERN}
            
            UTF-8
        
    


    

    
    
        
        ${log.path}/log_debug.log
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            UTF-8 
        
        
        
            
            ${log.path}/debug/log-debug-%d{yyyy-MM-dd}.%i.log
            
                ${MAXFILESIZE}
            
            
            ${MAXHISTORY}
        
        
        
            debug
            ACCEPT
            DENY
        
    

    
    
        
        ${log.path}/log_info.log
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            UTF-8
        
        
        
            
            ${log.path}/info/log-info-%d{yyyy-MM-dd}.%i.log
            
                ${MAXFILESIZE}
            
            
            ${MAXHISTORY}
        
        
        
            info
            ACCEPT
            DENY
        
    

    
    
        
        ${log.path}/log_warn.log
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            UTF-8 
        
        
        
            ${log.path}/warn/log-warn-%d{yyyy-MM-dd}.%i.log
            
                ${MAXFILESIZE}
            
            
            ${MAXHISTORY}
        
        
        
            warn
            ACCEPT
            DENY
        
    


    
    
        
        ${log.path}/log_error.log
        
        
            %d{yyyy-MM-dd HH:mm:ss.SSS} [%thread] %-5level %logger{50} - %msg%n
            UTF-8 
        
        
        
            ${log.path}/error/log-error-%d{yyyy-MM-dd}.%i.log
            
                ${MAXFILESIZE}
            
            
            ${MAXHISTORY}
        
        
        
            ERROR
            ACCEPT
            DENY
        
    

    
    
    
    


    

    
    
        
    

    
        
        
        
        
        
    

    
    
        
            
            
            
            
            
        
    


参考:
Spring Boot 2.0 Release Notes
Spring Boot 2.0 Migration Guide

你可能感兴趣的:(springboot)