jar包:執行SpringBoot主類的main方法,啟動ioc容器,創建嵌入式的Servlet容器;
war包:啟動服務器,**服務器啟動SpringBoot應用**【SpringBootServletInitializer】,啟動ioc容器;
servlet3.0(Spring注解版):
8.2.4 Shared libraries / runtimes pluggability:
規則:
? 1)、服務器啟動(web應用啟動)會創建當前web應用里面每一個jar包里面ServletContainerInitializer實例:
? 2)、ServletContainerInitializer的實現放在jar包的META-INF/services文件夾下,有一個名為javax.servlet.ServletContainerInitializer的文件,內容就是ServletContainerInitializer的實現類的全類名
? 3)、還可以使用@HandlesTypes,在應用啟動的時候加載我們感興趣的類;
流程:
1)、啟動Tomcat
2)、org\springframework\spring-web\4.3.14.RELEASE\spring-web-4.3.14.RELEASE.jar!\META-INF\services\javax.servlet.ServletContainerInitializer:
Spring的web模塊里面有這個文件:**org.springframework.web.SpringServletContainerInitializer**
3)、SpringServletContainerInitializer將@HandlesTypes(WebApplicationInitializer.class)標注的所有這個類型的類都傳入到onStartup方法的Set<Class<?>>;為這些WebApplicationInitializer類型的類創建實例;
4)、每一個WebApplicationInitializer都調用自己的onStartup;

5)、相當于我們的SpringBootServletInitializer的類會被創建對象,并執行onStartup方法
6)、SpringBootServletInitializer實例執行onStartup的時候會createRootApplicationContext;創建容器
```java
protected WebApplicationContext createRootApplicationContext(
ServletContext servletContext) {
//1、創建SpringApplicationBuilder
SpringApplicationBuilder builder = createSpringApplicationBuilder();
StandardServletEnvironment environment = new StandardServletEnvironment();
environment.initPropertySources(servletContext, null);
builder.environment(environment);
builder.main(getClass());
ApplicationContext parent = getExistingRootWebApplicationContext(servletContext);
if (parent != null) {
this.logger.info("Root context already created (using as parent).");
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, null);
builder.initializers(new ParentContextApplicationContextInitializer(parent));
}
builder.initializers(
new ServletContextApplicationContextInitializer(servletContext));
builder.contextClass(AnnotationConfigEmbeddedWebApplicationContext.class);
//調用configure方法,子類重寫了這個方法,將SpringBoot的主程序類傳入了進來
builder = configure(builder);
//使用builder創建一個Spring應用
SpringApplication application = builder.build();
if (application.getSources().isEmpty() && AnnotationUtils
.findAnnotation(getClass(), Configuration.class) != null) {
application.getSources().add(getClass());
}
Assert.state(!application.getSources().isEmpty(),
"No SpringApplication sources have been defined. Either override the "
+ "configure method or add an @Configuration annotation");
// Ensure error pages are registered
if (this.registerErrorPageFilter) {
application.getSources().add(ErrorPageFilterConfiguration.class);
}
//啟動Spring應用
return run(application);
}
```
7)、Spring的應用就啟動并且創建IOC容器
```java
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
FailureAnalyzers analyzers = null;
configureHeadlessProperty();
SpringApplicationRunListeners listeners = getRunListeners(args);
listeners.starting();
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(
args);
ConfigurableEnvironment environment = prepareEnvironment(listeners,
applicationArguments);
Banner printedBanner = printBanner(environment);
context = createApplicationContext();
analyzers = new FailureAnalyzers(context);
prepareContext(context, environment, listeners, applicationArguments,
printedBanner);
//刷新IOC容器
refreshContext(context);
afterRefresh(context, applicationArguments);
listeners.finished(context, null);
stopWatch.stop();
if (this.logStartupInfo) {
new StartupInfoLogger(this.mainApplicationClass)
.logStarted(getApplicationLog(), stopWatch);
}
return context;
}
catch (Throwable ex) {
handleRunFailure(context, listeners, analyzers, ex);
throw new IllegalStateException(ex);
}
}
```
**==啟動Servlet容器,再啟動SpringBoot應用==**
- Spring Boot 入門
- Spring Boot 簡介
- 微服務
- 環境準備
- MAVEN設置
- IDEA設置
- Spring Boot HelloWorld
- 創建一個maven工程;(jar)
- 導入spring boot相關的依賴
- 編寫一個主程序;啟動Spring Boot應用
- 編寫相關的Controller、Service
- 運行主程序測試
- 簡化部署
- Hello World探究
- POM文件
- 父項目
- 啟動器
- 主程序類,主入口類
- 使用Spring Initializer
- IDEA使用 Spring Initializer
- STS使用 Spring Starter Project快速創建項目
- 配置文件
- 配置文件
- YAML語法
- 基本語法
- 值的寫法
- 普通的值(數字,字符串,布爾)
- 對象、Map(屬性和值)(鍵值對)
- 數組(List、Set)
- 配置文件值注入
- 其他問題
- properties配置文件在idea中默認utf-8可能會亂碼
- @Value獲取值和@ConfigurationProperties獲取值比較
- 配置文件注入值數據校驗
- @PropertySource&@ImportResource&@Bean
- 配置文件占位符
- 隨機數
- 占位符獲取之前配置的值
- Profile
- 多Profile文件
- yml支持多文檔塊方式
- 激活指定profile
- 配置文件加載位置
- 外部配置加載順序
- 自動配置原理
- 自動配置原理
- 細節
- @Conditional派生注解(Spring注解版原生的@Conditional作用)
- 日志
- 日志框架
- SLF4j使用
- 如何在系統中使用SLF4j
- 遺留問題
- SpringBoot日志關系
- 日志使用
- 默認配置
- 指定配置
- 切換日志框架
- Web開發
- 簡介
- SpringBoot對靜態資源的映射規則
- 模板引擎
- 引入thymeleaf
- Thymeleaf使用
- 語法規則
- SpringMVC自動配置
- Spring MVC auto-configuration
- 擴展SpringMVC
- 全面接管SpringMVC
- 如何修改SpringBoot的默認配置
- RestfulCRUD
- 默認訪問首頁
- 國際化
- 登陸
- 攔截器進行登陸檢查
- CRUD-員工列表
- thymeleaf公共頁面元素抽取
- CRUD-員工添加
- CRUD-員工修改
- CRUD-員工刪除
- 錯誤處理機制
- SpringBoot默認的錯誤處理機制
- 如果定制錯誤響應
- 如何定制錯誤的頁面
- 如何定制錯誤的json數據
- 將我們的定制數據攜帶出去
- 配置嵌入式Servlet容器
- 如何定制和修改Servlet容器的相關配置
- 注冊Servlet三大組件【Servlet、Filter、Listener】
- 替換為其他嵌入式Servlet容器
- 嵌入式Servlet容器自動配置原理
- 嵌入式Servlet容器啟動原理
- 使用外置的Servlet容器
- 步驟
- 原理
- Docker
- 簡介
- 核心概念
- 安裝Docker
- 安裝linux虛擬機
- 在linux虛擬機上安裝docker
- Docker常用命令&操作
- 鏡像操作
- 容器操作
- 安裝MySQL示例
- SpringBoot與數據訪問
- JDBC
- 整合Druid數據源
- 整合MyBatis
- 注解版
- 配置文件版
- 整合SpringData JPA
- SpringData簡介
- 整合SpringData JPA
- 啟動配置原理
- 創建SpringApplication對象
- 運行run方法
- 事件監聽機制
- 自定義starter