嵌入式servlet容器,打包成可执行的jar
优点:简单,便携
缺点:默认不支持jsp,优化定制比较复杂(使用定制器[ServerProperties,自定义定制器]),自己编写嵌入式servlet容器的创建工厂.
外置的servlet容器:外面安装tomcat--应用war包的方式打包
步骤:
- 必须创建一个war项目
- 将嵌入式的tomcat指定为provided
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
- 必须编写一个SpringBootServletInitializer 的子类并用configure方法
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
// 传入主程序
return application.sources(DemoApplication.class);
}
}
- 启动服务器
使用外部servlet容器启动springboot原理
jar包:执行springboot主类的main方法,启动ioc容器,创建嵌入式的servlet容器.
war包:启动服务器,服务器启动springboot应用[SpringBootServletInitializer],启动ioc容器.
servlet3.0规范:
(1) 服务器启动(web应用启动)会创建当前web应用里面每一个jar包里面servletContainerInitializer实例
(2) servletContainerInitializer的实现放在jar包的META-INF/services文件夹下,有一个名为javax.servlet.ServletContainerInitalizer的文件,内容就是servletContainerInitializer的实现类的全类名
(3) 还可以使用@HandlesTypes,在应用启动的时候加载我们感兴趣的类;
流程:
(1) 启动tomcat
(2) spring-web/5.1.9.RELEASE/spring-web-5.1.9.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
- 相当于我们的springBootServletIntializer的类会被创建对象,并执行onStartup方法.
- SpringBootServletINitalizer执行onStartup的时候,会createRootApplicationContext(servletContext);创建根容器
protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
// 创建构造器
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));
}
// 启动
return this.run(application);
}
- 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);
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应用