天天看点

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

继续阅读