SpringBoot使用外置Servlet的原理:
jar包:執行SpringBoot主類的main方法,啓動ioc容器,創建嵌入式的Servlet容器;
war包:啓動服務器,服務器啓動SpringBoot應用,啓動ioc容器;
我們可以找到這個類:ServletInitializer
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoOgj01Application.class);
}
}
Servlet3.0(Spring註解版):
規則:
1)、服務器啓動(web應用啓動)會創建當前web應用裏面每個jar包裏面ServletContainerInitalizer實例;
2)、ServletContainerInitalizer的實現放在jar包的META-INF/services文件下,有一個名爲javax.servlet.ServletContainerInitalizer的文件,內容的內容就是ServletContainerInitalizer的實現類的全類名;
3)、還可以使用@HandlesTyoes,在應用啓動的時候加載我們感興趣的class;
ctrl+H 查看接口下的實現類。
流程:
1)、啓動Tomcat
2)、在這個路徑下:org\springframework\spring-web\5.2.4.RELEASE\spring-web-5.2.4.RELEASE.jar!\META-INF\services\javax.servlet.ServletContainerInitializer
Spring的web模塊中有這個文件:org.springframework.web.SpringServletContainerInitializer
3)、SpringServletContainerInitializer將**@HandlesTypes({WebApplicationInitializer.class})標註的所有這個類型的類傳入到onStarup方法的Set**;爲這些WebApplicationInitializer類型的類創建實例;
4)、每個WebApplicationInitializer都調用自己的onStarup;
5)、我們又繼承了SpringBootServletInitializer,實現了以下函數,相當於我們的SpringBootServletInitializer的類會被創建對象,並執行onStarup方法;
- 下面這個這個函數會告訴SpringBootServletInitializer我們要啓動的Spring的IOC容器是哪一個,然後傳入到onStarup中。
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
return application.sources(DemoOgj01Application.class);
}
//這個函數會告訴SpringBootServletInitializer我們要啓動的Spring的IOC容器是哪一個,然後傳入到onStarup中。
}
6)、SpringBootServletInitializer的示例執行onStarup的時候會createRootApplicationContext,創建容器:
public void onStartup(ServletContext servletContext) throws ServletException {
this.logger = LogFactory.getLog(this.getClass());
WebApplicationContext rootAppContext = this.createRootApplicationContext(servletContext);
if (rootAppContext != null) {
servletContext.addListener(new ContextLoaderListener(rootAppContext) {
public void contextInitialized(ServletContextEvent event) {
}
});
} else {
this.logger.debug("No ContextLoaderListener registered, as createRootApplicationContext() did not return an application context");
}
}
執行createRootApplicationContext
protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
//1、創建SpringApplicationBuilder
SpringApplicationBuilder builder = this.createSpringApplicationBuilder();
builder.main(this.getClass());
ApplicationContext parent = this.getExistingRootWebApplicationContext(servletContext);
if (parent != null) {
this.logger.info("Root context already created (using as parent).");
servletContext.setAttribute(
WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, (Object)null);
builder.initializers(new ApplicationContextInitializer[]{new ParentContextApplicationContextInitializer(parent)});
}
builder.initializers(new ApplicationContextInitializer[]{new ServletContextApplicationContextInitializer(servletContext)});
builder.contextClass(AnnotationConfigServletWebServerApplicationContext.class);
//2.調用configure方法,子類重寫了這個方法,我們調用的是子類方法,將SpringBoot的主程序類傳入
builder = this.configure(builder);
builder.listeners(new ApplicationListener[]{new SpringBootServletInitializer.WebEnvironmentPropertySourceInitializer(servletContext)});
//3.使用builder創建一個Spring應用
SpringApplication application = builder.build();
if (application.getAllSources().isEmpty() && MergedAnnotations.from(this.getClass(), SearchStrategy.TYPE_HIERARCHY).isPresent(Configuration.class)) {
application.addPrimarySources(Collections.singleton(this.getClass()));
}
Assert.state(!application.getAllSources().isEmpty(), "No SpringApplication sources have been defined. Either override the configure method or add an @Configuration annotation");
if (this.registerErrorPageFilter) {
application.addPrimarySources(Collections.singleton(ErrorPageFilterConfiguration.class));
}
//4.啓動Spring
return this.run(application);
}
7)、Spring的應用就啓動了,並且創建IOC容器。
public ConfigurableApplicationContext run(String... args) {
StopWatch stopWatch = new StopWatch();
stopWatch.start();
ConfigurableApplicationContext context = null;
Collection<SpringBootExceptionReporter> exceptionReporters = new ArrayList();
this.configureHeadlessProperty();
SpringApplicationRunListeners listeners = this.getRunListeners(args);
listeners.starting();
Collection exceptionReporters;
try {
ApplicationArguments applicationArguments = new DefaultApplicationArguments(args);
ConfigurableEnvironment environment = this.prepareEnvironment(listeners, applicationArguments);
this.configureIgnoreBeanInfo(environment);
Banner printedBanner = this.printBanner(environment);
context = this.createApplicationContext();
exceptionReporters = this.getSpringFactoriesInstances(SpringBootExceptionReporter.class, new Class[]{ConfigurableApplicationContext.class}, context);
this.prepareContext(context, environment, listeners, applicationArguments, printedBanner);
//刷新IOC容器,IOC容器初始化
this.refreshContext(context);
this.afterRefresh(context, applicationArguments);
stopWatch.stop();
if (this.logStartupInfo) {
(new StartupInfoLogger(this.mainApplicationClass)).logStarted(this.getApplicationLog(), stopWatch);
}
listeners.started(context);
this.callRunners(context, applicationArguments);
} catch (Throwable var10) {
this.handleRunFailure(context, var10, exceptionReporters, listeners);
throw new IllegalStateException(var10);
}
try {
listeners.running(context);
return context;
} catch (Throwable var9) {
this.handleRunFailure(context, var9, exceptionReporters, (SpringApplicationRunListeners)null);
throw new IllegalStateException(var9);
}
}