spring項(xiàng)目升級(jí)springboot

插件準(zhǔn)備:maven helper 解決包沖突必備神器峭梳。
目標(biāo):將原始項(xiàng)目的spring 版本4.3.22.RELEASE,升級(jí)為springboot 的2.3.5.RELEASE版本,步驟如下:

  1. 加入springboot包飒焦,并且改war包為jar包

        <packaging>jar</packaging>
    
        <dependencyManagement>
            <dependencies>
                <dependency>
                    <!-- Import dependency management from Spring Boot -->
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-dependencies</artifactId>
                    <version>${version.springboot}</version>
                    <type>pom</type>
                    <scope>import</scope>
                </dependency>
            </dependencies>
        </dependencyManagement>
    
       <!--spring boot-->
            <dependency>
                <groupId>org.springframework</groupId>
                <artifactId>spring-beans</artifactId>
            </dependency>
            <dependency>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-starter-web</artifactId>
            </dependency>
    
  1. 同步springframework版本號(hào)典奉,也就是移除掉springframework系列包中指定的version,spring的版本會(huì)自動(dòng)和dependencyManagement的spring版本一致

  2. 創(chuàng)建啟動(dòng)類鳞芙,把原來(lái)的xml的配置文件包含進(jìn)去

    @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打包插件

                <plugin>
                    <groupId>org.springframework.boot</groupId>
                    <artifactId>spring-boot-maven-plugin</artifactId>
                    <configuration>
                        <mainClass>com.demo.manage.Application</mainClass>
                    </configuration>
                    <executions>
                        <execution>
                            <goals>
                                <goal>repackage</goal>
                            </goals>
                        </execution>
                    </executions>
                </plugin>
    
  1. 創(chuàng)建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配置:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:aop="http://www.springframework.org/schema/aop"
        xsi:schemaLocation="http://www.springframework.org/schema/beans
                            http://www.springframework.org/schema/beans/spring-beans.xsd
                            http://www.springframework.org/schema/aop
                            http://www.springframework.org/schema/aop/spring-aop.xsd">
     <aop:aspectj-autoproxy proxy-target-class="true" />
    
     <!-- disconf配置 -->
     <bean id="disconfMgrBean" class="com.baidu.disconf.client.DisconfMgrBean"
         destroy-method="destroy">
         <property name="scanPackage" value="com.demo.manage" />
     </bean>
     <bean id="disconfMgrBean2" class="com.baidu.disconf.client.DisconfMgrBeanSecond"
         init-method="init" destroy-method="destroy">
     </bean>
    
     <bean id="configproperties_no_reloadable_disconf"
         class="com.baidu.disconf.client.addons.properties.ReloadablePropertiesFactoryBean">
         <property name="locations">
             <list>
                 <value>redis.properties</value>
                 <value>dubbo.properties</value>
                 <value>kafka.properties</value>
                 <value>kafka.consumer.config.properties</value>
                 <value>db.properties</value>
    
             </list>
         </property>
     </bean>
    
     <bean id="hostBindProperties" class="com.demo.common.util.PropertiesFactoryBean">
         <property name="fileLists">
             <list>
                 <value>config/host-bind.properties</value>
             </list>
         </property>
     </bean>
    
     <!-- 使用托管方式的disconf配置(無(wú)代碼侵入, 配置更改不會(huì)自動(dòng)reload) -->
     <bean id="propertyConfigurerForNoReload"
         class="org.springframework.beans.factory.config.PropertyPlaceholderConfigurer">
         <property name="ignoreResourceNotFound" value="false" />
         <property name="ignoreUnresolvablePlaceholders" value="false" />
         <property name="propertiesArray">
             <list>
                 <ref bean="configproperties_no_reloadable_disconf" />
                 <ref bean="hostBindProperties" />
             </list>
         </property>
     </bean>
    </beans>
    

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啟動(dòng)包

        <dependency>
            <groupId>com.fcbox</groupId>
            <artifactId>spring-boot-starter-disconf</artifactId>
            <version>2.0.0</version>
        </dependency>
  1. 修改環(huán)境變量配置命名規(guī)范驯嘱。

    • 將pom中的profiles.activation修改為profile.active

    • logback文件按照配置環(huán)境區(qū)分配置:

          <springProfile name="dev,local,per,pet,uat,uat1,uat2,uat3,uat4,uat5,uat6,uat7">
              <root level="INFO">
                  <appender-ref ref="CONSOLE"/>
                  <appender-ref ref="notify_log"/>
                  <appender-ref ref="error_log"/>
              </root>
          </springProfile>
          <springProfile name="prd,stg">
              <root level="INFO">
                  <appender-ref ref="notify_log"/>
                  <appender-ref ref="error_log"/>
              </root>
          </springProfile>
      
  2. 第三方sdk包中使用classpath讀取不到配置文件,可以使用絕對(duì)路徑加載喳坠,如:

     <bean id="personalPaymentService" class="com.demo.payment.client.impl.PersonalPaymentServiceImpl" >
         <property name="configPath" value="file:/app/spring-boot/demo-server/disconf/payment_common.properties" />
     </bean>
    
  3. 包沖突問題

    • jackson版本沖突鞠评,報(bào)錯(cuò)信息:Caused by: java.lang.NoClassDefFoundError: com/fasterxml/jackson/datatype/jsr310/ser/ZoneIdSerializer。直接升級(jí)到最高版本2.12.0丙笋,然后干掉版本不一致的jackson包,涉及的jackson包為:

              <dependency>
                  <groupId>com.fasterxml.jackson.core</groupId>
                  <artifactId>jackson-core</artifactId>
                  <version>2.12.0</version>
              </dependency>
              <dependency>
                  <groupId>com.fasterxml.jackson.core</groupId>
                  <artifactId>jackson-databind</artifactId>
                  <version>2.12.0</version>
                  <exclusions>
                      <exclusion>
                          <artifactId>jackson-core</artifactId>
                          <groupId>com.fasterxml.jackson.core</groupId>
                      </exclusion>
                      <exclusion>
                          <artifactId>jackson-annotations</artifactId>
                          <groupId>com.fasterxml.jackson.core</groupId>
                      </exclusion>
                  </exclusions>
              </dependency>
              <dependency>
                  <groupId>com.fasterxml.jackson.core</groupId>
                  <artifactId>jackson-annotations</artifactId>
                  <version>2.12.0</version>
              </dependency>
      
      
    • log4j包缺失報(bào)錯(cuò) java.lang.ClassNotFoundException: org.apache.log4j.Logger谢澈。dubbo使用了 log4j所以需要引入log4j包:

              <dependency>
                  <groupId>org.slf4j</groupId>
                  <artifactId>log4j-over-slf4j</artifactId>
                  <version>1.7.30</version>
              </dependency>
      
    • jedis包沖突 java.lang.ClassNotFoundException: redis.clients.jedis.util.Pool 排除掉jedis低版本包

  1. Redis配置讀取本地文件煌贴,需要改為Spring-map加載

    <bean id="mallResourcePropertySource"
         class="org.springframework.core.io.support.ResourcePropertySource">
       <constructor-arg name="name" value="mall-redis-conf.properties"/>
       <constructor-arg name="resource"
                    value="classpath:mall-redis-conf.properties"/>
    </bean>
    
    上面代碼改為
    <bean id="resourcePropertySource"
         class="org.springframework.core.env.PropertiesPropertySource">
       <constructor-arg name="name" value="mall-redis-conf.properties"/>
       <constructor-arg name="source">
          <map>
             <entry key="spring.redis.cluster.max-redirects" value="${spring.redis.cluster.max-redirects}" />
             <entry key="spring.redis.cluster.nodes" value="${spring.redis.cluster.nodes}"/>
          </map>
       </constructor-arg>
    </bean>
    
    

從spring升級(jí)到springboot并不復(fù)雜,本來(lái)springboot就是spring框架的擴(kuò)展锥忿,只需要把spring的版本號(hào)和springboot中的保持一致牛郑,注意下classpath的打包路徑和解決掉依賴的沖突包即可

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市敬鬓,隨后出現(xiàn)的幾起案子淹朋,更是在濱河造成了極大的恐慌,老刑警劉巖钉答,帶你破解...
    沈念sama閱讀 222,946評(píng)論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件础芍,死亡現(xiàn)場(chǎng)離奇詭異,居然都是意外死亡数尿,警方通過(guò)查閱死者的電腦和手機(jī)仑性,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,336評(píng)論 3 399
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)右蹦,“玉大人诊杆,你說(shuō)我怎么就攤上這事『温剑” “怎么了晨汹?”我有些...
    開封第一講書人閱讀 169,716評(píng)論 0 364
  • 文/不壞的土叔 我叫張陵,是天一觀的道長(zhǎng)贷盲。 經(jīng)常有香客問我淘这,道長(zhǎng),這世上最難降的妖魔是什么巩剖? 我笑而不...
    開封第一講書人閱讀 60,222評(píng)論 1 300
  • 正文 為了忘掉前任铝穷,我火速辦了婚禮,結(jié)果婚禮上佳魔,老公的妹妹穿的比我還像新娘氧骤。我一直安慰自己,他們只是感情好吃引,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,223評(píng)論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著刽锤,像睡著了一般镊尺。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上并思,一...
    開封第一講書人閱讀 52,807評(píng)論 1 314
  • 那天庐氮,我揣著相機(jī)與錄音,去河邊找鬼宋彼。 笑死弄砍,一個(gè)胖子當(dāng)著我的面吹牛仙畦,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播音婶,決...
    沈念sama閱讀 41,235評(píng)論 3 424
  • 文/蒼蘭香墨 我猛地睜開眼慨畸,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了衣式?” 一聲冷哼從身側(cè)響起寸士,我...
    開封第一講書人閱讀 40,189評(píng)論 0 277
  • 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎碴卧,沒想到半個(gè)月后弱卡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 46,712評(píng)論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡住册,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,775評(píng)論 3 343
  • 正文 我和宋清朗相戀三年婶博,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片荧飞。...
    茶點(diǎn)故事閱讀 40,926評(píng)論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡凡人,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出垢箕,到底是詐尸還是另有隱情划栓,我是刑警寧澤,帶...
    沈念sama閱讀 36,580評(píng)論 5 351
  • 正文 年R本政府宣布条获,位于F島的核電站忠荞,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏帅掘。R本人自食惡果不足惜委煤,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,259評(píng)論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望修档。 院中可真熱鬧碧绞,春花似錦、人聲如沸吱窝。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,750評(píng)論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)院峡。三九已至兴使,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間照激,已是汗流浹背发魄。 一陣腳步聲響...
    開封第一講書人閱讀 33,867評(píng)論 1 274
  • 我被黑心中介騙來(lái)泰國(guó)打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人励幼。 一個(gè)月前我還...
    沈念sama閱讀 49,368評(píng)論 3 379
  • 正文 我出身青樓汰寓,卻偏偏與公主長(zhǎng)得像,于是被迫代替她去往敵國(guó)和親苹粟。 傳聞我的和親對(duì)象是個(gè)殘疾皇子有滑,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,930評(píng)論 2 361

推薦閱讀更多精彩內(nèi)容