SpringBoot 使用外部Tomcat方法及啟動原理
SpringBoot 使用外部Tomcat方法及啟動原理
基於 SpringBoot 2.x
方法
- 一、必須是一個war專案,利用IDEA可以直接建立,或者是修改pom.xml檔案
<packaging>war</packaging>
- 二、將內建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 builder) {
// 傳入SpringBoot的主程式類
return builder.sources(SbdemoApplication.class);
}
}
啟動原理
前提
- 根據Servlet3.0規範,伺服器啟動(web應用啟動)會建立當前web應用裡面每一個jar包裡面ServletContainerInitializer例項。
- ServletContainerInitializer的實現放在jar包的META-INF/services資料夾下,有一個名為javax.servlet.ServletContainerInitializer的檔案,內容就是ServletContainerInitializer的實現類的全類名。
- 使用@HandlesTypes,在應用啟動的時候載入我們感興趣的類。
SpringBoot中啟動流程
- 一、SpringBoot 中的 ServletContainerInitializer 實現類位置在spring-web模組下
檔案內容:
org.springframework.web.SpringServletContainerInitializer
- 二、SpringServletContainerInitializer類
@HandlesTypes({WebApplicationInitializer.class})
public class SpringServletContainerInitializer implements ServletContainerInitializer {
public SpringServletContainerInitializer() {
}
public void onStartup(@Nullable Set<Class<?>> webAppInitializerClasses, ServletContext servletContext) throws ServletException {
List<WebApplicationInitializer> initializers = new LinkedList();
Iterator var4;
if (webAppInitializerClasses != null) {
var4 = webAppInitializerClasses.iterator();
while(var4.hasNext()) {
Class<?> waiClass = (Class)var4.next();
if (!waiClass.isInterface() && !Modifier.isAbstract(waiClass.getModifiers()) && WebApplicationInitializer.class.isAssignableFrom(waiClass)) {
try {
// 將@HandlesTypes(WebApplicationInitializer.class)標註的所有這個型別的類都傳入到onStartup方法的Set<Class<?>>;為這些WebApplicationInitializer型別的類建立例項。
initializers.add((WebApplicationInitializer)ReflectionUtils.accessibleConstructor(waiClass, new Class[0]).newInstance());
} catch (Throwable var7) {
throw new ServletException("Failed to instantiate WebApplicationInitializer class", var7);
}
}
}
}
if (initializers.isEmpty()) {
servletContext.log("No Spring WebApplicationInitializer types detected on classpath");
} else {
servletContext.log(initializers.size() + " Spring WebApplicationInitializers detected on classpath");
AnnotationAwareOrderComparator.sort(initializers);
var4 = initializers.iterator();
while(var4.hasNext()) {
WebApplicationInitializer initializer = (WebApplicationInitializer)var4.next();
// 每一個WebApplicationInitializer都呼叫自己的onStartup()
initializer.onStartup(servletContext);
}
}
}
}
- 三、Tomcat 啟動引導類
// ServletInitializer 繼承 SpringBootServletInitializer
public class ServletInitializer extends SpringBootServletInitializer {
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(SbdemoApplication.class);
}
}
SpringBootServletInitializer 繼承 WebApplicationInitializer 類,就是SpringServletContainerInitializer類的@HandlesTypes({WebApplicationInitializer.class}), 下面展示的是 onStartup() 、createRootApplicationContext() 和 configure() 方法
public abstract class SpringBootServletInitializer implements WebApplicationInitializer {
public void onStartup(ServletContext servletContext) throws ServletException {
this.logger = LogFactory.getLog(this.getClass());
// 建立web應用容器
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");
}
}
protected WebApplicationContext createRootApplicationContext(ServletContext servletContext) {
// 1、建立SpringApplicationBuilder
SpringApplicationBuilder builder = this.createSpringApplicationBuilder();
StandardServletEnvironment environment = new StandardServletEnvironment();
// 2、準備環境
environment.initPropertySources(servletContext, (ServletConfig)null);
builder.environment(environment);
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)});
}
// 3、初始化
builder.initializers(new ApplicationContextInitializer[]{new ServletContextApplicationContextInitializer(servletContext)});
builder.contextClass(AnnotationConfigServletWebServerApplicationContext.class);
// 4、呼叫configure方法,子類重寫了這個方法,將SpringBoot的主程式類傳入了進來
builder = this.configure(builder);
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));
}
// 5、啟動Spring應用
return this.run(application);
}
}
// 此方法被啟動引導類 ServletInitializer有方法重寫, 傳入的是應用構建器SpringApplicationBuilder, 也就是SpringBoot的主程式類
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder;
}
個人隨筆,如有錯誤,歡迎指正!
相關文章
- springboot2.0使用外部tomcat進行啟動方法Spring BootTomcat
- SpringBoot使用外部tomcatSpring BootTomcat
- springboot使用外部tomcat啟動,整合jsp,另有整合dubbo樣例Spring BootTomcatJS
- 配置springboot專案使用外部tomcatSpring BootTomcat
- SpringBoot使用IDEA設定的外部Tomcat啟動,遇到的問題和解決Spring BootIdeaTomcat
- Springboot整合外部Tomcat配置Spring BootTomcat
- SpringBoot中配置外部TomcatSpring BootTomcat
- SpringBoot 之配置外部TomcatSpring BootTomcat
- 使用springboot建立web專案,並使用外部TomcatSpring BootWebTomcat
- SpringBoot配置外部Tomcat專案啟動流程原始碼分析(長文)Spring BootTomcat原始碼
- springboot 專案部署到外部tomcatSpring BootTomcat
- SpringBoot配置外部Tomcat並支援JSPSpring BootTomcatJS
- SpringBoot啟動原理Spring Boot
- SpringBoot 啟動原理Spring Boot
- SpringBootApplication是如何啟動Tomcat的? | 破解SpringBoot Tomcat啟動之謎 !Spring BootAPPTomcat
- SpringBoot配置外部Tomcat並打war包Spring BootTomcat
- springboot 整合外部tomcat war包部署方式Spring BootTomcat
- springBoot-啟動原理Spring Boot
- SpringBoot 啟動類的原理Spring Boot
- Tomcat在SpringBoot中是如何啟動的TomcatSpring Boot
- springboot之啟動原理解析Spring Boot
- SpringBoot啟動流程分析原理(一)Spring Boot
- springboot自動配置原理和啟動流程Spring Boot
- SpringBoot原始碼解析-內嵌Tomcat容器的啟動Spring Boot原始碼Tomcat
- Springboot2專案配置(熱部署+war+外部tomcat+外部配置檔案)Spring Boot熱部署Tomcat
- tomcat無法啟動的解決方法Tomcat
- springboot系列文章之啟動原理詳解Spring Boot
- SpringBoot原始碼學習4——SpringBoot內嵌Tomcat啟動流程原始碼分析Spring Boot原始碼Tomcat
- [MySQL] “MySQL 服務無法啟動”原理及解決方法MySql
- SpringBoot外部化配置使用Plus版Spring Boot
- SpringBoot部署到外部Tomcat無法註冊到Nacos服務端Spring BootTomcat服務端
- Tomcat 7 啟動分析(三)Digester 的使用Tomcat
- 【Tomcat】Tomcat工作原理及簡單模擬實現Tomcat
- Linux之換源、Tomcat及jdk的安裝配置和設定Tomcat自動啟動LinuxTomcatJDK
- Tomcat 7 啟動分析(五)Lifecycle 機制和實現原理Tomcat
- 【SpringBoot】服務 Jar 包的啟動過程原理Spring BootJAR
- tomcat 啟動失敗Tomcat
- SpringBoot使用外部Web容器的解決方案Spring BootWeb