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 源码解析 更新完毕

源码解析

598d58a0ae756e372d70a682e5d71b1c.png

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

说在最后

本次解析仅代表个人观点,仅供参考。

e402cf7d97f05eb56a68943ff440531a.png

扫码进入技术微信群

5ed1b62f20e53da46c4f772d472f896c.png e831cf29d6eef22454ba7f03116f0880.png d74bcb3380735ef8946e60a120b04b68.png钉钉技术群

ff5d01f7b2c08e3bca2c7674556d418e.png

qq技术群

0371349424222b2f38c660fda28f8408.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值