27 外部Servlet启动SpringBoot原理

版权声明:看什么?6,你和我,走一波! https://blog.csdn.net/qq_31323797/article/details/84870649

1 jar与war启动

jar包:
	执行SpringBoot主类的main方法,启动ioc容器,嵌入式的Servlet容器跟随启动;

war包:
	启动服务器,服务器启动SpringBoot应用SpringBootServletInitializer,启动ioc容器

2 启动流程

2.1 启动Tomcat

查找所有jar包下META-INF/services文件夹下javax.servlet.ServletContainerInitializer

2.2 SpringServletContainerInitializer

将@HandlesTypes(WebApplicationInitializer.class)标注的所有这个类型的类都传入到onStartup方法的Set<Class<?>>;为这些WebApplicationInitializer类型的类创建实例
@HandlesTypes({WebApplicationInitializer.class})
public class SpringServletContainerInitializer implements ServletContainerInitializer {
    public SpringServletContainerInitializer() {
    }

    public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException {
        ...
}

2.3 每个WebApplicationInitializer都调用本身的onStartup

2.4 相当于SpringBootServletInitializer的类会被创建对象,并执行onStartup方法

2.5 SpringBootServletInitializer实例执行onStartup的时候会createRootApplicationContext,创建容器

protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {

	// 创建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);
	
	// 调用configure方法,子类重写这个方法,将SpringBoot的主程序类传入了进来
	builder = this.configure(builder);
	builder.listeners(new ApplicationListener[]{new SpringBootServletInitializer.WebEnvironmentPropertySourceInitializer(servletContext)});
	
	// 使用builder创建一个Spring应用
	SpringApplication application = builder.build();
	if (application.getAllSources().isEmpty() && AnnotationUtils.findAnnotation(this.getClass(), Configuration.class) != null) {
		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));
	}

	// 启动Spring应用
	return this.run(application);
}

2.6 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容器
		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);
	}
}
  • 启动Servlet容器,再启动SpringBoot应用

猜你喜欢

转载自blog.csdn.net/qq_31323797/article/details/84870649
27
今日推荐