javascript
Spring Boot(二)——项目热部署与程序发布
一、項目熱部署
1.1 配置依賴
① pom.xml加入devtools依賴,如果scope是provided則無法實現熱部署,參考。
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-devtools</artifactId><scope>compile</scope><optional>true</optional> </dependency>② maven插件配置如下。
<build><plugins><plugin><groupId>org.springframework.boot</groupId><artifactId>spring-boot-maven-plugin</artifactId><configuration><fork>true</fork></configuration></plugin></plugins> </build>1.2 設置自動構建項目
在Settings/Build->Execution->Deployment/Compiler中勾選Build project automatically,如圖所示。
1.3 Register配置
① 組合快捷鍵(Ctrl+Shift+Alt+/),彈出如下所示。
② 選擇Register,找到并選中compiler.automake.allow.when.app.running,重啟IDEA。
1.4 測試
重啟IDEA后,啟動項目,修改HelloController。
修改前代碼如下:
改后代碼如下:
@RequestMapping(value = "sayHello", method = RequestMethod.GET) public String sayHello() {return "Hello Spring Boot!"; }改前后效果對比。
二、程序發布
2.1 以jar形式發布
① 默認情況下,打包形式為jar。
<groupId>com.peter</groupId> <artifactId>springboot-first-demo</artifactId> <version>0.0.1-SNAPSHOT</version> <packaging>jar</packaging>② 通過Maven執行install,項目相關的依賴也會打到一個jar里面。
③ 進入項目的target文件夾,cmd下運行java -jar **.jar即可。
默認情況下,測試類代碼如下:
@RunWith(SpringRunner.class) @SpringBootTest public class SpringbootFirstDemoApplicationTests {@Testpublic void contextLoads() {}}報錯如下:
[ERROR] Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.022 s <<< FAILURE! - in com.peter.SpringbootFirstDemoApplicationTests
[ERROR] initializationError(com.peter.SpringbootFirstDemoApplicationTests) Time elapsed: 0.003 s <<< ERROR!
java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=…) with your test
根據提示,需要在SpringbootFirstDemoApplicationTests類上的注解加上@SpringBootTest(classes = SpringbootFirstDemoApplicationTests.class)。加上后,再次執行install即可打包成功。
2.2 以war形式發布
該部分參考。
① 通過Maven執行clean,清除原有的jar包或者war包;修改pom.xml中packing的值為war,代碼如下:
② 添加tomcat的依賴:
<dependency><groupId>org.springframework.boot</groupId><artifactId>spring-boot-starter-tomcat</artifactId> </dependency>③ 修改啟動類文件,使其繼承SpringBootServletInitializer類,并重寫configure函數。代碼如下:
package com.peter.springbootfirstdemo;import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.boot.builder.SpringApplicationBuilder; import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;@SpringBootApplication public class SpringbootFirstDemoApplication extends SpringBootServletInitializer{public static void main(String[] args) {SpringApplication.run(SpringbootFirstDemoApplication.class, args);}@Overrideprotected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {return builder.sources(SpringbootFirstDemoApplication.class);} }④ 通過Maven執行install,在target文件夾中生成war包。
⑤ 將springboot-first-demo-0.0.1-SNAPSHOT.war拷貝到Tomcat的webapps文件夾下,啟動Tomcat。
⑥ 訪問http://localhost:8080/springboot-first-demo-0.0.1-SNAPSHOT/sayHello。
總結
以上是生活随笔為你收集整理的Spring Boot(二)——项目热部署与程序发布的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: KBQA_多轮对话——模型源码解析(一)
- 下一篇: DM8达梦数据库存储过程函数使用