這里寫目錄標題
- Spring Boot 簡介
- 微服務
- 環境準備
- maven設定
- IDEA設定
- 使用SpringBoot創建一個HellWorld應用
- 1、創建一個maven工程(spring-boot-01-helloworld)
- 2、在pom.xml中匯入spring boot相關的依賴
- 3、撰寫一個主程式
- 4、撰寫相關的Controller
- 5、運行主程式
- 簡化部署
Spring Boot 簡介
Spring Boot是由Pivotal團隊提供的全新框架,其設計目的是用來簡化新Spring應用的初始搭建以及開發程序,該框架使用了特定的方式來進行配置,從而使開發人員不再需要定義樣板化的配置,通過這種方式,Spring Boot致力于在蓬勃發展的快速應用開發領域(rapid application development)成為領導者,
- 簡化Spring應用開發的一個框架
- 整個Spring技術堆疊的一個大整合
- J2EE開發的一站式解決方案
微服務
微服務:每一個功能元素最終都是一個可獨立替換和獨立升級的軟體單元,
詳情參考:微服務檔案
環境準備
- jdk1.8:Spring Boot 推薦jdk1.7及以上
- maven3.x:maven 3.3以上版本
- IntelliJIDEA:或者STS
- SpringBoot 1.5.9.RELEASE:1.5.10
maven設定
在maven 的settings.xml組態檔的profiles標簽添加以下配置:
<profile>
<id>jdk‐1.8</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>1.8</jdk>
</activation>
<properties>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
<maven.compiler.compilerVersion>1.8</maven.compiler.compilerVersion>
</properties>
</profile>
IDEA設定
把maven整合到idea,

使用SpringBoot創建一個HellWorld應用
功能:瀏覽器發送hello請求,服務器接受請求并處理,回應
Hello World字串,
1、創建一個maven工程(spring-boot-01-helloworld)
專案目錄:

2、在pom.xml中匯入spring boot相關的依賴
<parent>
<artifactId>spring-boot-dependencies</artifactId>
<groupId>org.springframework.boot</groupId>
<version>1.5.10.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
3、撰寫一個主程式
HelloWorldMainApplication:
package com.keafmd;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* Keafmd
*
* @ClassName: HelloWorldMainApplication
* @Description: 主程式
* @author: 牛哄哄的柯南
* @date: 2021-02-22 15:00
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//Spring應用啟動起來
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
4、撰寫相關的Controller
HelloController:
package com.keafmd.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
/**
* Keafmd
*
* @ClassName: HelloController
* @Description:
* @author: 牛哄哄的柯南
* @date: 2021-02-22 15:04
*/
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}
5、運行主程式

運行結果:

打開瀏覽器訪問:http://localhost:8080/hello

OK,至此,第一個SpringBoot的HelloWorld就大功告成了,【amazing~】
簡化部署
1、我們在pom.xml檔案中假如以下代碼:
<!-- 這個插件,可以將應用打包成一個可執行的jar包 -->
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<executions>
<execution>
<goals>
<goal>repackage</goal>
</goals>
</execution>
</executions>
</plugin>
</plugins>
</build>
2、然后,我們將應用打包

3、然后再target檔案夾下就可以看到spring-boot-01-helloworld-1.0-SNAPSHOT.jar

4、復制到桌面(隨便哪,個人選擇),打開cmd視窗,切換到jar包所在位置,我的是桌面,然后輸入:java -jar spring-boot-01-helloworld-1.0-SNAPSHOT.jar,運行效果如下,
5、打開瀏覽器訪問:http://localhost:8080/hello,同樣可以看到HelloWord

這樣的部署就變得十分簡單了,
以上就是SpringBoot入門教程(超詳細)的全部內容,
看完如果對你有幫助,感謝點贊支持!
如果你是電腦端的話,看到右下角的 “一鍵三連” 了嗎,沒錯點它[哈哈]

加油!
共同努力!
Keafmd
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/262549.html
標籤:java
上一篇:由淺入深了解JVM-虛擬機堆疊
