一、Spring Boot 入門
1、Spring Boot 簡介
- 簡化 Spring 應用開發的一個框架;
- 整個Spring技術棧的一個大整合;
- J2EE開發的一站式解決方案;
2、微服務
- 2014 - martin fowler 提出
- 微服務:架構風格(服務微化)
- 一個應用應該是一組小型服務;可以通過HTTP的方式進行互通;
- 單體應用:ALL IN ONE
- 微服務:每一個功能元素最終都是一個可獨立替換和獨立升級的軟件單元;
1、MAVEN設置
給 maven 的 settings.xml 配置文件的 profiles 標籤添加 (也就是設置 maven 支持的默認jdk版本)
<profile>
<id>jdk-11</id>
<activation>
<activeByDefault>true</activeByDefault>
<jdk>11</jdk>
</activation>
<properties>
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<maven.compiler.compilerVersion>11</maven.compiler.compilerVersion>
</properties>
</profile>
2、創建一個maven工程;(jar)
3、導入 spring boot 相關的依賴
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
</dependencies>
4、編寫一個主程序;啓動 Spring Boot 應用
/**
* @Author: Mr_OO
* @Date: 2020/1/10 15:08
* 標明這是一個主程序類
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
//Spring應用啓動起來
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
5、編寫相關的Controller、Service
@Controller
public class HelloController {
@ResponseBody
@RequestMapping("/hello")
public String hello(){
return "Hello World!";
}
}
6、運行主程序測試
7、簡化部署
<!-- 這個插件,可以將應用打包成一個可執行的jar包;-->
<build>
<plugins>
<plugin>
<groupId>org.springframework .boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
將這個應用打成 jar 包,直接使用 java -jar 的命令進行執行;
Hello World探究
1、POM文件
1、父項目
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.2.1.RELEASE</version>
</parent>
他的父項目是
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-dependencies</artifactId>
<version>2.2.1.RELEASE</version>
<relativePath>../../spring-boot-dependencies</relativePath>
</parent>
他來真正管理Spring Boot應用裏面的所有依賴版本;
Spring Boot的版本仲裁中心;
以後我們導入依賴默認是不需要寫版本;(沒有在dependencies裏面管理的依賴自然需要聲明版本號)
2、啓動器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
spring-boot-starter-web:
spring-boot-starter:spring-boot場景啓動器;幫我們導入了web模塊正常運行所依賴的組件;
SpringBoot 將所有的功能場景都抽取出來,做成一個個的starters(啓動器),只需要在項目裏面引入這些starter相關場景的所有依賴都會導入進來。要用什麼功能就導入什麼場景的啓動器。
2、主程序類,主入口類
/**
* @SpringBootApplication 來標註一個主程序類,說明這是一個Spring Boot應用
*/
@SpringBootApplication
public class HelloWorldMainApplication {
public static void main(String[] args) {
// Spring應用啓動起來
SpringApplication.run(HelloWorldMainApplication.class,args);
}
}
@SpringBootApplication: Spring Boot應用標註在某個類上說明這個類是 SpringBoot 的主配置類,SpringBoot就應該運行這個類的main方法來啓動SpringBoot應用;
@Target(ElementType.TYPE)
@Retention(RetentionPolicy.RUNTIME)
@Documented
@Inherited
@SpringBootConfiguration
@EnableAutoConfiguration
@ComponentScan(excludeFilters = {
@Filter(type = FilterType.CUSTOM, classes = TypeExcludeFilter.class),
@Filter(type = FilterType.CUSTOM, classes = AutoConfigurationExcludeFilter.class) })
public @interface SpringBootApplication {
@SpringBootConfiguration : Spring Boot的配置類;
標註在某個類上,表示這是一個Spring Boot的配置類;
@Configuration : 配置類上來標註這個註解;
配置類 ----- 配置文件;配置類也是容器中的一個組件;@Component
@EnableAutoConfiguration:開啓自動配置功能;
以前我們需要配置的東西,Spring Boot幫我們自動配置;@EnableAutoConfiguration告訴SpringBoot開啓自動配置功能;這樣自動配置才能生效;
@AutoConfigurationPackage
@Import(EnableAutoConfigurationImportSelector.class)
public @interface EnableAutoConfiguration {
@AutoConfigurationPackage:自動配置包
@Import(AutoConfigurationPackages.Registrar.class):
Spring的底層註解@Import,給容器中導入一個組件;導入的組件由AutoConfigurationPackages.Registrar.class;
將主配置類(@SpringBootApplication標註的類)的所在包及下面所有子包裏面的所有組件掃描到Spring容器;
@Import(EnableAutoConfigurationImportSelector.class);
給容器中導入組件
EnableAutoConfigurationImportSelector:導入哪些組件的選擇器;
將所有需要導入的組件以全類名的方式返回;這些組件就會被添加到容器中;
會給容器中導入非常多的自動配置類(xxxAutoConfiguration);就是給容器中導入這個場景需要的所有組件,並配置好這些組件;
有了自動配置類,免去了我們手動編寫配置注入功能組件等的工作;
SpringFactoriesLoader.loadFactoryNames(EnableAutoConfiguration.class,classLoader);
Spring Boot 在啓動的時候從類路徑下的 META-INF/spring.factories 中獲取 EnableAutoConfiguration 指定的值,將這些值作爲自動配置類導入到容器中,自動配置類就生效,幫我們進行自動配置工作; 以前我們需要自己配置的東西,自動配置類都幫我們配置好了;
J2EE 的整體整合解決方案和自動配置都在spring-boot-autoconfigure-2.2.1.RELEASE.jar;
使用 Spring Initializer 快速創建 Spring Boot 項目
1、IDEA:使用 Spring Initializer 快速創建項目
IDE都支持使用Spring的項目創建嚮導快速創建一個 Spring Boo t項目;
選擇我們需要的模塊;嚮導會聯網創建 Spring Boot 項目;
默認生成的 Spring Boot 項目;
- 主程序已經生成好了,我們只需要我們自己的邏輯
- resources文件夾中目錄結構
- static:保存所有的靜態資源; js css images 等;
- templates:保存所有的模板頁面;( Spring Boot 默認 jar 包使用嵌入式的 Tomcat ,默認不支持 JSP 頁面);可以使用模板引擎(freemarker、thymeleaf);
- application.properties:Spring Boot 應用的配置文件;可以修改一些默認設置;