SpringBoot微服務框架
springboot
- 是什麼?
- 配置如何編寫 yaml
- 自動裝配原理
- 整合Web開發
- 整合資料庫Druid
- 分散式開發:Dubbo(RPC)+zookeeper
- swagger:介面文件
- 任務排程
- SpringSecurity : Shiro
自動裝填原理
pom.xml
核心依賴在父工程中:<artifactId>spring-boot-dependencies</artifactId>
我們在寫或者引入一下SpringBoot依賴的時候,不需要指定版本,就因為有這些版本倉庫
啟動器
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter</artifactId>
</dependency>
Springboot的啟動環境
例如:spring-boot-starter-web,它就會幫我們自動匯入web環境的所有依賴
Springboot會將所有的功能,都變成一個個的啟動器
如果要是有什麼功能,只需找到對應的啟動器即可
spring-boot-starter:核心模組,包括自動配置支援、日誌和YAML;
spring-boot-starter-test:測試模組,包括JUnit、Hamcrest、Mockito;
@RestController的意思就是controller裡面的方法都以json格式輸出;
主程式
//@SpringBootApplication : 標註這個類是SpringBoot的應用
@SpringBootApplication
public class Springboot01Application {
public static void main(String[] args) {
// 將SpringBoot應用啟動
SpringApplication.run(Springboot01Application.class, args);
}
}
註解 @SpringBootConfiguration:SpringBoot的配置 @Configuration:spring配置類 @Component:spring元件 @EnableAutoConfiguration:自動配置 @AutoConfigurationPackage:自動配置包 @Import(AutoConfigurationPackages.Registrar.class):自動配置'包註冊' @Import(AutoConfigurationImportSelector.class):自動配置選擇器 獲取所有的配置 List<String> configurations = getCandidateConfigurations(annotationMetadata, attributes); 配置候選的配置 protected List<String> getCandidateConfigurations(AnnotationMetadata metadata, AnnotationAttributes attributes) { List<String> configurations = SpringFactoriesLoader.loadFactoryNames(getSpringFactoriesLoaderFactoryClass(), getBeanClassLoader()); Assert.notEmpty(configurations, "No auto configuration classes found in META-INF/spring.factories. If you " + "are using a custom packaging, make sure that file is correct."); return configurations; }
META-INF/spring.factories自動配置的核心檔案
結論:springboot所有的自動配置都是在啟動的時候掃描並載入spring.factories所有的自動配置類都在這個裡面,但是不一定生效要判斷條件是否成立,只要匯入了對應的start就有對應的啟動器,有了啟動器,自動裝配就會生效,然後配置成功
步驟:
1.springboot在啟動的時候,從類路徑下/META-INF/spring.factories獲取指定的值
2.將這些自動匯入配置的類匯入容器,自動配置就會生效,幫我進行自動配置
3.一起我們需要自動配置的東西,現在springboot幫我們做了
4.整合JavaEE,解決方案和自動配置的東西都在spring-boot-autoconfigure-2.3.5.RELEASE.jar這個包下面
5.它會把所有需要匯入的元件,以類名的方式返回,這些元件就會新增到容器;
6.容器中也會存在非常多的xxxAutoConfiguration的檔案(@Bean),就是這些類給容器中匯入了這個場景需要的所有元件