天天看點

servletcontext_springboot源碼架構解析servletContext

說在前面

前期回顧

sharding-jdbc源碼解析 更新完畢

spring源碼解析 更新完畢

spring-mvc源碼解析 更新完畢

spring-tx源碼解析 更新完畢

spring-boot源碼解析 更新完畢

rocketmq源碼解析 更新完畢

dubbbo源碼解析 更新完畢

netty源碼解析 更新完畢

spring源碼架構更新完畢

spring-mvc源碼架構更新完畢

springboot源碼架構更新中

github https://github.com/tianheframe

sharding-jdbc源碼解析 更新完畢

rocketmq源碼解析 更新完畢

seata 源碼解析 更新完畢

dubbo 源碼解析 更新完畢

netty 源碼解析 更新完畢

源碼解析

servletcontext_springboot源碼架構解析servletContext

org.springframework.boot.web.servlet.ServletContextInitializer接口,用于以程式設計方式配置Servlet 3.0+上下文。與WebApplicationInitializer不同,實作這個接口的類(不實作WebApplicationInitializer)不會被SpringServletContainerInitializer檢測到,是以Servlet容器不會自動引導它們。這個接口主要設計為允許由Spring管理servletcontextinitialalizer,而不是Servlet容器。

void onStartup(ServletContext servletContext) throws ServletException;
           

使用初始化所需的任何servlet、過濾器、偵聽器上下文參數和屬性配置給定的ServletContext。

org.springframework.boot.context.embedded.InitParameterConfiguringServletContextInitializer 配置ServletContext的初始化參數的ServletContextInitializer

private final Map<String, String> parameters;
           

初始化參數

@Override  public void onStartup(ServletContext servletContext) throws ServletException {    for (Entry<String, String> entry : this.parameters.entrySet()) {      servletContext.setInitParameter(entry.getKey(), entry.getValue());    }  }
           

設定初始化參數

org.springframework.boot.context.embedded.jetty.ServletContextInitializerConfiguration jetty配置的ServletContextInitializer

private final ServletContextInitializer[] initializers;
           

initializers

@Override  public void configure(WebAppContext context) throws Exception {    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();    Thread.currentThread().setContextClassLoader(context.getClassLoader());    try {      callInitializers(context);    }    finally {      Thread.currentThread().setContextClassLoader(classLoader);    }  }
           

org.springframework.boot.context.embedded.jetty.ServletContextInitializerConfiguration#callInitializers

private void callInitializers(WebAppContext context) throws ServletException {    try {      setExtendedListenerTypes(context, true);      for (ServletContextInitializer initializer : this.initializers) {//        執行org.springframework.boot.web.servlet.ServletContextInitializer.onStartup()方法        initializer.onStartup(context.getServletContext());      }    }    finally {      setExtendedListenerTypes(context, false);    }  }
           

org.springframework.boot.context.embedded.jetty.ServletContextInitializerConfiguration#setExtendedListenerTypes

private void setExtendedListenerTypes(WebAppContext context, boolean extended) {    try {      context.getServletContext().setExtendedListenerTypes(extended);    }    catch (NoSuchMethodError ex) {      // Not available on Jetty 8    }  }
           

調用ServletContextInitializer

說在最後

本次解析僅代表個人觀點,僅供參考。

servletcontext_springboot源碼架構解析servletContext

掃碼進入技術微信群

servletcontext_springboot源碼架構解析servletContext
servletcontext_springboot源碼架構解析servletContext
servletcontext_springboot源碼架構解析servletContext

釘釘技術群

servletcontext_springboot源碼架構解析servletContext

qq技術群

servletcontext_springboot源碼架構解析servletContext

繼續閱讀