來源:KL博客
http://www.kailing.pub/article/index/arcid/188.html
1、前言碎語
博主公司一個專案在開發中使用某些功能的時候,受限于spring低版本的限制,故索性將整個模塊升級為spring boot,在這里做個記錄,希望能幫助到有相同場景的朋友,
整個改造程序非常簡單,耗時大概在2個小時左右,主要解決專案中的各種版本沖突,不過下面我會介紹一個神器,
2、老專案情況
1.專案使用spring-context作為容器,使用RabbitMQ提供RPC服務
2.spring.springframework 版本比較低,3.1.x的版本,升級后會變成4.3.x
3.專案使用maven構建
以上是專案的基本情況,針對如上情況,下面會詳細描述改造程序中需要的關注點,
第一步:添加spring boot依賴
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.7.RELEASE</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-aop</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-cache</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-amqp</artifactId>
</dependency>
</dependencies>
第二步:****新增spring boot啟動類,加載原先的xml配置
/**
* Created by kl on 2018/1/29.
* Content :Lbt-service-ext服務啟動器
*/@SpringBootApplication(exclude = {RabbitAutoConfiguration.class})@ImportResource("service-context.xml")publicclassLbtServiceExtApplication{
publicstaticvoid main(String[] args) {
SpringApplication application= newSpringApplication(LbtServiceExtApplication.class);
application.setWebEnvironment(false);
application.run(args);
}}
注意的地方:
1.排除了RabbitMQ的自動裝載了,因為在xml中已經配置過了RabbitMQ的相關連接和服務資訊了
2.設定了setWebEnvironment(false),標記專案為非web專案,因為只是提供RPC服務,所以不需要servlet容器,
第三步:****嘗試啟動,排除jar沖突
這個時候可以啟動main方法,看看能否啟動了,一般情況下沒那么容易就能啟動起來,會有各種的jar沖突,我們專案從3.x到4.x,更是各種沖突,
下面介紹一個插件,破除jar沖突排除的煩惱,前提是在IDEA下開發,eclipse應該也有類似的,
插件名字:****Maven Helper
可以代替mvn dependency:tree命令的使用了,這個插件可以更直觀的列出專案依賴的jar,非常牛逼的是可以直接列出專案中有沖突的jar,這對找jar沖突非常有用,而且可以直接右鍵排除掉,
jar相關例外識別技巧:
出現NoSuchMethodError:一般都是jar沖突了
出現ClassNotFoundException:缺少相關的jar了
三步做完后,專案妥妥的跑起來了,
3、Spring Boot怎么識別web專案
1.spring boot會識別專案是否是web專案,如果識別到事web專案,又沒有添加tomcat等容器jar,就拋例外,
2.識別的方式就是看專案是否依賴了servlet-api和spring-web,而我們專案需要spring-web相關如el等功能又不需要tomcat容器,所以可以指定為非web專案,
3.排除掉tomcat后,專案jar體積和運行時記憶體占用都有很大的改善,
推薦去我的博客閱讀更多:
1.Java JVM、集合、多執行緒、新特性系列教程
2.Spring MVC、Spring Boot、Spring Cloud 系列教程
3.Maven、Git、Eclipse、Intellij IDEA 系列工具教程
4.Java、后端、架構、阿里巴巴等大廠最新面試題
覺得不錯,別忘了點贊+轉發哦!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/179246.html
標籤:Java
上一篇:引入mybatis-plus報 Invalid bound statement錯誤怎么辦,動動手指改一個地方就行
下一篇:匿名實作類&匿名物件
