本項目代碼地址:demo-world (spring-boot-demo模塊)
這裏是spring-boot模塊的第一期文章,主要來講一下spring-boot是什麼?怎麼用?爲什麼要用?
1.What is SpringBoot?
官方文檔開頭說明了這個項目的目的:
Our primary goals are:
Provide a radically faster and widely accessible getting-started experience for all Spring development.
Be opinionated out of the box but get out of the way quickly as requirements start to diverge from the defaults.
Provide a range of non-functional features that are common to large classes of projects (such as embedded servers, security, metrics, health checks, and externalized configuration).
Absolutely no code generation and no requirement for XML configuration.
簡而言之,spring-boot具有一下幾個特點:
- 使用大量的默認配置代替手動配置,(如果有特別需要個別項也可以自定義配置)
- 使用.yml代替xml配置,增加了配置的可讀性
- 內置tomcat,因此可以使用jar包部署,不必使用war包(也不需要額外的tomcat環境)
2.一個極簡的SpringBoot項目:
接下來我將用三段代碼(本例的全部代碼)來呈現有個項目:
首先是maven的pom.xml配置:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.3.0.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.swing</groupId>
<artifactId>spring-boot-demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>spring-boot-demo</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>1.8</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
<scope>runtime</scope>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
然後是一個接口:
@RestController
@RequestMapping("/first")
public class FirstController {
@RequestMapping("/")
String home() {
return "Hello World!";
}
}
最後是項目的啓動入口:
@SpringBootApplication
public class Main {
public static void main(String[] args) {
SpringApplication.run(Main.class, args);
System.out.println("啓動成功啦!");
}
}
OK ! 這就是用Spring Boot 搭建的一個web項目的全部了,炒雞簡單吧!甚至我們連tomcat都不需要了
spring是如何做到這一點的呢?答案在 @EnableAutoConfiguration註解中(此註解包含在@SpringBootApplication註解中)官方文檔對它的解釋是:
The second class-level annotation is
@EnableAutoConfiguration
. This annotation tells Spring Boot to “guess” how you want to configure Spring, based on the jar dependencies that you have added. Sincespring-boot-starter-web
added Tomcat and Spring MVC, the auto-configuration assumes that you are developing a web application and sets up Spring accordingly.
總的來說,springBoot會根據你的依賴自動揣摩你這大概是個什麼類型的項目,然後使用一些常用的配置來配置此項目(例如需要使用 tomcat ,默認端口爲8080等)
接下來討論一下主方法,這是項目的運行入口,通過調用run委託給Spring Boot的SpringApplication類。run方法的args參數意在獲取運行時傳入的參數,例如使用 --spring.profiles.active 指定運行環境(開發,測試,部署)
3.更自由的配置springBoot
體驗過上面的快捷配置,但仔細想想,是否有一種配置可以在簡便的同時更加自由,確實有,那就是 使用application.yml
官網爲我們給出給出了所有的默認的配置:鏈接 (當然,這裏面不包括一些第三方的框架對spring-boot的支持,例如spring-boot-mybatis等)
擴展一點:上面提到過一個運行環境(dev,test,prod),項目在不同環境下運行需要不同的 application.yml 配置,我們通常使用如下配置文件名:
- applicaiton-dev.yml
- application-test.yml
- application-prod.yml
當需要指定某一個運行環境時候,只需要如下寫法即可:
java -jar (要運行的jar包地址)--spring.profiles.active=dev