1、什么是SpringBoot
SpringBoot
是基於Spring
的基礎上提供了一套全新的框架,其目的是為了在開發時簡化Spring
的相關配置及開發過程。在SpringBoot
未出來之前,准備搭建一個Spring
的開發環境需要配置一堆的XML
文件,而SpringBoot
就是去除了大量的XML
配置文件,簡化了復雜的依賴管理。
Spring Boot
集成了大量常用的第三方庫配置,Spring Boot
應用中這些第三方庫幾乎可以是零配置的開箱即用(out-of-the-box
),大部分的Spring Boot
應用都只需要非常少量的配置代碼(基於Java
的配置),開發者能夠更加專注於業務邏輯。
2、SpringBoot特征
- 獨立運行的
Spring
項目,使用jar
包的形式獨立運行,只需通過命令java -jar xx.jar
即可運行。 - 內嵌
Servlet
容器(例如Tomcat
、Jetty
或者Undertow
等),應用無需打成WAR
包 。 - 提供
starter
簡化Maven
配置,提供了一系列的starter
項目對象模型(POMS
)來簡化Maven
配置。 - 提供了大量的默認自動配置,來簡化項目的開發,開發人員也通過配置文件修改默認配置。
- 自帶應用監控(如指標、健康檢查和外部化配置)。
- 沒有代碼生成和
XML
配置。
3、快速搭建SpringBoot
- 引入
maven
依賴
首先引入parent
依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.5.3</version>
</parent>
因為是要開發一個web
項目,因此需要引入web
依賴。
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- 創建啟動類
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class,args);
}
}
- 創建
controller
類
@RestController
@RequestMapping(path = "/v1/index")
public class IndexController {
@GetMapping(path = "")
public String index(){
return "Hello Spring Boot!";
}
}
- 啟動項目
Connected to the target VM, address: '127.0.0.1:54766', transport: 'socket'
. ____ _ __ _ _
/\\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.5.3)
2021-08-04 23:16:55.694 INFO 15528 --- [ main] com.tenghu.sb.Application : Starting Application using Java 1.8.0_301 on Arvin with PID 15528 (E:\project\java\spring-boot-study\target\classes started by admin in E:\project\java\spring-boot-study)
2021-08-04 23:16:55.696 INFO 15528 --- [ main] com.tenghu.sb.Application : No active profile set, falling back to default profiles: default
2021-08-04 23:16:56.231 INFO 15528 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2021-08-04 23:16:56.237 INFO 15528 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2021-08-04 23:16:56.237 INFO 15528 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.50]
2021-08-04 23:16:56.292 INFO 15528 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2021-08-04 23:16:56.292 INFO 15528 --- [ main] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 567 ms
2021-08-04 23:16:56.503 INFO 15528 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path ''
2021-08-04 23:16:56.509 INFO 15528 --- [ main] com.tenghu.sb.Application : Started Application in 1.065 seconds (JVM running for 1.607)
2021-08-04 23:17:07.919 INFO 15528 --- [nio-8080-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring DispatcherServlet 'dispatcherServlet'
2021-08-04 23:17:07.919 INFO 15528 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Initializing Servlet 'dispatcherServlet'
2021-08-04 23:17:07.920 INFO 15528 --- [nio-8080-exec-1] o.s.web.servlet.DispatcherServlet : Completed initialization in 1 ms
看到這個結果,表示SpringBoot
項目就已經啟動成功了,現在使用瀏覽器訪問路徑http://localhost:8080/v1/index
就可以看到瀏覽器輸出結果:Hello Spring Boot!
。
以上是手動建立一個空的maven
項目,手動添加依賴的方式創建一個簡單的spring boot
項目,另外還有兩種方式可以快速生成spring boot
項目:
- 使用IDEA,里面已經集成了SpringBoot插件
- 使用官網提供的創建SpringBoot插件。https://start.spring.io/