spring项目升级springboot

插件准备:maven helper 解决包冲突必备神器。
目标:将原始项目的spring 版本4.3.22.RELEASE,升级为springboot 的2.3.5.RELEASE版本,步骤如下:

  1. 加入springboot包,并且改war包为jar包

        jar
    
        
            
                
                    
                    org.springframework.boot
                    spring-boot-dependencies
                    ${version.springboot}
                    pom
                    import
                
            
        
    
       
            
                org.springframework
                spring-beans
            
            
                org.springframework.boot
                spring-boot-starter-web
            
    
  1. 同步springframework版本号,也就是移除掉springframework系列包中指定的version,spring的版本会自动和dependencyManagement的spring版本一致

  2. 创建启动类,把原来的xml的配置文件包含进去

    @SpringBootApplication(exclude = {RedisRepositoriesAutoConfiguration.class, DataSourceAutoConfiguration.class, RedisAutoConfiguration.class, KafkaAutoConfiguration.class, MongoDataAutoConfiguration.class, MongoRepositoriesAutoConfiguration.class})
    @ImportResource(locations = {"/spring/applicationContext.xml"})
    public class Application {
        public static void main(String[] args) {
            SpringApplication.run(CustNotifyApplication .class, args);
        }
    }
    

    增加springboot打包插件

                
                    org.springframework.boot
                    spring-boot-maven-plugin
                    
                        com.demo.manage.Application
                    
                    
                        
                            
                                repackage
                            
                        
                    
                
    
  1. 创建application.yaml

    server:
      port: 8080
      shutdown: graceful
    spring:
      application:
        name: demo-server
      profiles:
        active: ${profile.active}
      main:
        allow-bean-definition-overriding: true
      jackson:
        time-zone: GMT+8
    
  1. 干掉host-bind.properties,把dubbo host的配置放到application.yaml

    server:
      port: 8080
      shutdown: graceful
    spring:
      application:
        name: demo-server
      profiles:
        active: ${profile.active}
      main:
        allow-bean-definition-overriding: true
      jackson:
        time-zone: GMT+8
    
    
    dubbo:
      protocol:
        host: 127.0.0.1
    
    
  1. 原先的disconf是xml方式配置的,看得不舒服,索性干掉了下面的xml配置:

    
    
     
    
     
     
         
     
     
     
    
     
         
             
                 redis.properties
                 dubbo.properties
                 kafka.properties
                 kafka.consumer.config.properties
                 db.properties
    
             
         
     
    
     
         
             
                 config/host-bind.properties
             
         
     
    
     
     
         
         
         
             
                 
                 
             
         
     
    
    

application.yaml中加入disconf配置:

server:
  port: 8080
  shutdown: graceful
spring:
  application:
    name: demo-server
  profiles:
    active: ${profile.active}
  main:
    allow-bean-definition-overriding: true
  jackson:
    time-zone: GMT+8



dubbo:
  protocol:
    host: 127.0.0.1


# disconf config
disconf:
  scanPackage: com.demo.manage
  version: ${disconf.version}
  env: ${disconf.env}
  conf_server_host: ${disconf.server.host}
  files: redis.properties,dubbo.properties,kafka.properties,kafka.consumer.config.properties,db.properties
  app: demo-server
  debug: false
  enable:
    remote:
      conf: true
  conf_server_url_retry_times: 3
  conf_server_url_retry_sleep_seconds: 5
  user_define_download_dir: /app/spring-boot/demo-server/disconf
  enable_local_download_dir_in_class_path: false

在pom中加入了springboot的disconf启动包

        
            com.fcbox
            spring-boot-starter-disconf
            2.0.0
        
  1. 修改环境变量配置命名规范。

    • 将pom中的profiles.activation修改为profile.active

    • logback文件按照配置环境区分配置:

          
              
                  
                  
                  
              
          
          
              
                  
                  
              
          
      
  2. 第三方sdk包中使用classpath读取不到配置文件,可以使用绝对路径加载,如:

     
         
     
    
  3. 包冲突问题

    • jackson版本冲突,报错信息:Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/datatype/jsr310/ser/ZoneIdSerializer。直接升级到最高版本2.12.0,然后干掉版本不一致的jackson包,涉及的jackson包为:

              
                  com.fasterxml.jackson.core
                  jackson-core
                  2.12.0
              
              
                  com.fasterxml.jackson.core
                  jackson-databind
                  2.12.0
                  
                      
                          jackson-core
                          com.fasterxml.jackson.core
                      
                      
                          jackson-annotations
                          com.fasterxml.jackson.core
                      
                  
              
              
                  com.fasterxml.jackson.core
                  jackson-annotations
                  2.12.0
              
      
      
    • log4j包缺失报错 java.lang.ClassNotFoundException: org.apache.log4j.Logger。dubbo使用了 log4j所以需要引入log4j包:

              
                  org.slf4j
                  log4j-over-slf4j
                  1.7.30
              
      
    • jedis包冲突 java.lang.ClassNotFoundException: redis.clients.jedis.util.Pool 排除掉jedis低版本包

  1. Redis配置读取本地文件,需要改为Spring-map加载

    
       
       
    
    
    上面代码改为
    
       
       
          
             
             
          
       
    
    
    

从spring升级到springboot并不复杂,本来springboot就是spring框架的扩展,只需要把spring的版本号和springboot中的保持一致,注意下classpath的打包路径和解决掉依赖的冲突包即可

你可能感兴趣的:(spring项目升级springboot)