背景:公司最近在做項目升級,融合所有項目,但是目前使用的一個系統還是最原始的框架 springMVC+spring+mybatis ,前端還是jsp,easyui(技術老的掉牙),終於出手了,結果。。。就讓我開始修改。
前言:首先是百度一波,看看有沒有什么前車之鑒,然而失望而歸,感覺都不是很符合
開干:
第一步:首先在pom文件中添加spring-boot-starter相關依賴
如下:
<!-- spring boot --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <!--<version>2.1.1.RELEASE</version>--> <scope>provided</scope> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-legacy</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-actuator</artifactId> </dependency> <dependency> <groupId>org.apache.tomcat.embed</groupId> <artifactId>tomcat-embed-jasper</artifactId> <!--<version>9.0.14</version>--> <scope>provided</scope> </dependency> <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>
第二步:新增啟動類Application
@SpringBootApplication @ImportResource(locations = {"classpath:spring-*.xml","classpath:mybatis-config.xml"}) @ServletComponentScan public class xxSpringBootApplication { public static void main(String[] args) { SpringApplication application= new SpringApplication(T6SpringBootApplication.class); application.setWebEnvironment(true); application.run(args); System.out.println("xxxxxxxxxxxxxSB"); } }
有人問,為什么要加最后一行代碼,表達了憤怒的心情,控制台如果看到這句話,說明啟動成功了,但是經驗告訴我,看到這句話是何等的困難,都是淚
第三步:神器 Maven Helper
點擊啟動按鈕,當然是一堆報錯,都是些什么NoClassDefFoundError之類的。。。這些基本都是依賴沖突的問題。
這時候一個非常牛逼的插件就開始起作用了,Maven Helper 使用方法參考 https://blog.csdn.net/u013870094/article/details/79712500
這篇文章少了一步:右擊可以去除沖突
接着就是一系列的解決沖突的問題,還有一些依賴是缺少的,這個時候就需要去maven上獲取 https://mvnrepository.com/search ,需要的是耐心
第四步:
如果項目包含前端是jsp的,需要引入一些jsp的依賴....
第五步:
如果原來項目中有一個監聽類,是繼承 ContextLoaderListener 類的話,是需要修改的.
老代碼:
新代碼:
@WebListener public class SpringContextLoaderListener implements ServletContextListener { private static boolean initialized = false; @Override public void contextInitialized(ServletContextEvent event) { System.setProperty("thumbnailator.conserveMemoryWorkaround", "true"); // super.contextInitialized(event); initialized = true; } @Override public void contextDestroyed(ServletContextEvent event) { // super.contextDestroyed(event); } public static boolean isInitialized() { return initialized; } }
花了一天,終於項目正常運行了,可能還有一些隱藏的bug還沒有被發現,去測試了,。...........
補充:項目正常運行,發現無法原來使用的log4j打印日志,在springboot項目上無法打印日志。。坑爹。 springboot的打印日志組件變成了 logback,一頓改,先引入logback相關依賴包,然后將log4j.properties轉換成logback支持的logback.xml 。這里有一個官方的轉換網站 :https://logback.qos.ch/translator/ 。
最坑爹的就是轉換過的文件,控制台可以正常輸出日志,但是無法將日志輸出到文件,找了一下午,必須要添加下圖中紅色區域代碼
Ending!!!!!!!!!!!!!!!!!