Spring Boot如何移除內(nèi)嵌Tomcat,使用非web方式啟動
前言:當(dāng)我們使用Spring Boot編寫了一個批處理應(yīng)用程序,該程序只是用于后臺跑批數(shù)據(jù),此時不需要內(nèi)嵌的tomcat,簡化啟動方式使用非web方式啟動項目,步驟如下:
1、修改pom.xml文件
在pom.xml文件中去除內(nèi)嵌tomcat,添加servlet依賴
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> <exclusions> <!--去除內(nèi)嵌tomcat --> <exclusion> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> </exclusion> </exclusions> </dependency> <!--添加servlet的依賴--> <dependency> <groupId>javax.servlet</groupId> <artifactId>javax.servlet-api</artifactId> <version>3.1.0</version> <scope>compile</scope> </dependency>
2、設(shè)置打包方式
在pom.xml文件中將打項目包方式設(shè)置成jar,打成jar包通過命令去執(zhí)行jar
<packaging>jar</packaging>
3、禁用web程序啟動方式
對于非Web應(yīng)用程序,請在屬性文件中禁用Web應(yīng)用程序類型,application.yml文件中添加:
spring: main: web-application-type: none
4、在啟動類中擴展
繼承SpringBootServletInitializer 類,以下本人寫了一個測試方法,項目啟動后生成一個txt文件進行測試
@SpringBootApplication public class TestiopojApplication extends SpringBootServletInitializer { public static void main(String[] args) { System.out.println("項目開始啟動,開始執(zhí)行任務(wù)============"); SpringApplication.run(TestiopojApplication.class, args); String file = "E:\\copyFile";//文件存放路徑 String fileName = "test測試";//生成的文件名 String strContext = "測試成功=======";//文件內(nèi)容 try { FileUtils.writeStringToFile((new File(file + File.separator + fileName + ".txt")), strContext, "UTF-8"); System.out.println("文件創(chuàng)建成功============"); } catch (IOException e) { System.out.println("文件創(chuàng)建失敗============"); } } }
5、實列測試結(jié)果
由此我們可以通過java -jar 運行打包后的項目jar,控制臺顯示Spring Boot啟動標志,項目正常啟動,文件也正常創(chuàng)建成功,大功告成
以上就是Spring Boot如何移除內(nèi)嵌Tomcat,使用非web方式啟動的詳細內(nèi)容,更多關(guān)于Spring Boot移除內(nèi)嵌Tomcat的資料請關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Spring?Boot?教程之創(chuàng)建項目的三種方式
這篇文章主要分享了Spring?Boot?教程之創(chuàng)建項目的三種方式,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下2022-05-05Spring調(diào)度框架EnableScheduling&Scheduled源碼解析
這篇文章主要介紹了Spring調(diào)度框架EnableScheduling&Scheduled源碼解析,@EnableScheduling&Scheduled定時調(diào)度框架,本著不僅知其然還要知其所以然的指導(dǎo)思想,下面對該調(diào)度框架進行源碼解析,以便更好的理解其執(zhí)行過程,需要的朋友可以參考下2024-01-01druid監(jiān)控?zé)o法關(guān)閉的坑以及處理方式
這篇文章主要介紹了druid監(jiān)控?zé)o法關(guān)閉的坑以及處理方式,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教2024-05-05詳解Springboot Oauth2 Server搭建Oauth2認證服務(wù)
這篇文章主要介紹了Springboot Oauth2 Server 搭建Oauth2認證服務(wù),小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2019-05-05SpringMVC框架post提交數(shù)據(jù)庫出現(xiàn)亂碼解決方案
這篇文章主要介紹了SpringMVC框架post提交數(shù)據(jù)庫出現(xiàn)亂碼解決方案,文中通過示例代碼介紹的非常詳細,對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2020-09-09