SpringBoot簡介
概述
SpringBoot是Spring生態的一個模塊,現在有2個版本,但是SpringBoot2版本的更新幅度非常大,而且小版本更新也非常快!
在spring官網下劃有2段話,就能看出Spring Boot的地位
With Spring Boot in your app, just a few lines of code is all you need to start building services like a boss.
--在應用程式中使用Spring Boot,僅需要幾行代碼就可以構建服務
Originally [Netflix's Java] libraries and frameworks were built in-house.
I'm very proud to say, as of early 2019, we've moved our platform almost entirely over to Spring Boot.”
作用
Spring Boot makes it easy to create stand-alone,
production-grade Spring based Applications that you can "just run".
--能快速創建出生產級別的Spring應用
特性
- Create stand-alone Spring applications
創建獨立的Spring應用程式
- Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
直接嵌入Tomcat,Jetty或Undertow(無需部署WAR檔案)
- Provide opinionated 'starter' dependencies to simplify your build configuration
自動starter依賴,簡化構建配置
- Automatically configure Spring and 3rd party libraries whenever possible
自動配置Spring以及第三方功能
- Provide production-ready features such as metrics, health checks, and externalized configuration
提供生產級別的監控、健康檢查及外部化配置
- Absolutely no code generation and no requirement for XML configuration
無代碼生成、無需撰寫XML
參考地址
SpringBoot官網地址:https://spring.io/projects/spring-boot
SpringBoot版本說明:https://github.com/spring-projects/spring-boot/wiki#release-notes
SpringBoot結構

SpringBoot入門
SpringBoot入門可以參考地址:
https://docs.spring.io/spring-boot/docs/2.3.7.RELEASE/reference/html/getting-started.html#getting-started
系統要求
| 工具 | 說明 |
|---|---|
| JDK | java8,兼容Java15 |
| Maven | 3.3+ |
| 或者Gradle | 6(6.3或更高版本) |
創建maven工程
引入依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
創建主程式
@SpringBootApplication
public class MainApplication {
public static void main(String[] args) {
SpringApplication.run(MainApplication.class, args);
}
}
撰寫業務
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello() {
return "hello SpringBoot!";
}
}
運行程式
執行主程式,瀏覽器發送:http://localhost:8080/hello
組態檔
web程式中有各種配置比如埠等,SpringBoot有默認值,但是也可以創建組態檔進行更改
配置的參考地址:https://docs.spring.io/spring-boot/docs/2.3.7.RELEASE/reference/html/appendix-application-properties.html#common-application-properties
比如:創建application.properties檔案
server.port=9999
此時瀏覽器發送的地址為:http://localhost:9999/hello
簡化部署
pom.xml
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
然后就可以把專案打成jar包,之后直接在目標的服務器執行即可!
上面是用maven創建的專案然后進行撰寫,在IDE中可以使用專案初始化向導(Spring Initailizr)




入門程式的說明
看一下引入的pom依賴
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.7.RELEASE</version>
點進去可見有一個父專案的依賴
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.3.7.RELEASE</version>
接著點進去就可以看見里面有各種依賴,在里面搜索spring-boot-starter-web,可以看見
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<version>2.3.7.RELEASE</version>
</dependency>
ps:看見各種的spring-boot-starter-*,這是SpringBoot內置的啟動器,表示此場景下所需的依賴都進行匯入
starter參考地址:
https://docs.spring.io/spring-boot/docs/2.3.7.RELEASE/reference/html/using-spring-boot.html#using-boot-starter
看一下引入spring-boot-starter-web之后的jar包繼承圖

總結一下SpringBoot的自動配置特點
-
內嵌tomcat服務器
<dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-tomcat</artifactId> <version>2.3.7.RELEASE</version> <scope>compile</scope> </dependency> -
具有SpringMVC的功能,也就是SpringMVC的常用組件默認也都能做
<dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>5.2.12.RELEASE</version> <scope>compile</scope> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-webmvc</artifactId> <version>5.2.12.RELEASE</version> <scope>compile</scope> </dependency> -
默認的包結構:主程式所在包及其下面的所有子包里面的組件都會被默認掃描進來,依靠的就是@SpringBootApplication注解

-
具有默認配置
引入所需的場景starter之后,就有了此starter的默認配置,是以類展現的
比如:
public class Tomcat { protected Server server; protected int port = 8080; protected String hostname = "localhost"; protected String basedir; ------------------------------------------------------------ @ConfigurationProperties( prefix = "spring.servlet.multipart", ignoreUnknownFields = false ) public class MultipartProperties { private boolean enabled = true; private String location; private DataSize maxFileSize = DataSize.ofMegabytes(1L); private DataSize maxRequestSize = DataSize.ofMegabytes(10L); private DataSize fileSizeThreshold = DataSize.ofBytes(0L); private boolean resolveLazily = false;
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/240758.html
標籤:Java
上一篇:Spring-前言序
下一篇:Spring-IOC注解編程
