前言:當我們使用Spring Boot撰寫了一個批處理應用程式,該程式只是用于后臺跑批資料,此時不需要內嵌的tomcat,簡化啟動方式使用非web方式啟動專案,步驟如下:
1、修改pom.xml檔案
在pom.xml檔案中去除內嵌tomcat,添加servlet依賴
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<!--去除內嵌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、設定打包方式
在pom.xml檔案中將打專案包方式設定成jar,打成jar包通過命令去執行jar
<packaging>jar</packaging>
3、禁用web程式啟動方式
對于非Web應用程式,請在屬性檔案中禁用Web應用程式型別,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("專案開始啟動,開始執行任務============"); SpringApplication.run(TestiopojApplication.class, args); String file = "E:\\copyFile";//檔案存放路徑 String fileName = "test測驗";//生成的檔案名 String strContext = "測驗成功=======";//檔案內容 try { FileUtils.writeStringToFile((new File(file + File.separator + fileName + ".txt")), strContext, "UTF-8"); System.out.println("檔案創建成功============"); } catch (IOException e) { System.out.println("檔案創建失敗============"); } } }
5、實列測驗結果
由此我們可以通過java -jar 運行打包后的專案jar,控制臺顯示Spring Boot啟動標志,專案正常啟動,檔案也正常創建成功,大功告成!

文章參考:點我點我點我
個人總結:
我是南國以南i記錄點滴每天成長一點點,學習是永無止境的!轉載請附原文鏈接!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/264061.html
標籤:Java
上一篇:死磕Spring之IoC篇 - BeanDefinition 的決議程序(面向注解)
下一篇:什么是JIT?怎么優化?
