Tomcat原理系列之四:tomcat与spring容器的关系

简介: 在springboot盛行的今天,你是否还记得那,在xml文件中配置各种servlet, filter的日子。是否还记得那Tomat+spring+springmvc配置的组合。还有那熟悉的web.xml文件。不知你当时是否有过为何如此配置的疑惑?你又是否已经解惑。不要带着疑惑让他们远去。我们一起回顾

熟悉的web.xml


ContextLoaderListener

为了使用spring我们常见在web.xml中做这样的配置. 配置一个ContextLoaderListener监听器。这个监听器是如何把Tomcat与spring关联的呢?

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"  
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">  
    <filter>  
        <filter-name>characterEncodingFilter</filter-name>  
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>  
        <init-param>  
            <param-name>encoding</param-name>  
            <param-value>UTF-8</param-value>  
        </init-param>  
        <init-param>  
            <param-name>forceEncoding</param-name>  
            <param-value>true</param-value>  
        </init-param>  
    </filter>  
    <filter-mapping>  
        <filter-name>characterEncodingFilter</filter-name>  
        <url-pattern>/*</url-pattern>  
    </filter-mapping>  
    <!--启用spring-->
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
    <!--2、部署applicationContext的xml文件--> 
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>classpath:spring/applicationContext.xml</param-value>  
    </context-param>  
    <!--3、启用springmvc--> 
    <servlet>  
        <servlet-name>DispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>classpath:spring/dispatcher-servlet.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup><!--是启动顺序,让这个Servlet随Servletp容器一起启动。-->  
    </servlet>  
    <servlet-mapping>  
        <servlet-name>DispatcherServlet</servlet-name>  
        <url-pattern>/</url-pattern> <!--会拦截URL中带“/”的请求。-->  
    </servlet-mapping>  
    <welcome-file-list><!--指定欢迎页面-->  
        <welcome-file>login.html</welcome-file>  
    </welcome-file-list>  
    <error-page> <!--当系统出现404错误,跳转到页面nopage.html-->  
        <error-code>404</error-code>  
        <location>/nopage.html</location>  
    </error-page>  
    <error-page> <!--当系统出现java.lang.NullPointerException,跳转到页面error.html-->  
        <exception-type>java.lang.NullPointerException</exception-type>  
        <location>/error.html</location>  
    </error-page>  
    <session-config><!--会话超时配置,单位分钟-->  
        <session-timeout>360</session-timeout>  
    </session-config>  
</web-app>


Tomcat的初始化StandardContext.startInternal()


1.Tomcat对web.xml的加载.

要解开Tomcat与spring的关系,我们首先应该先搞懂,Tomcat是在什么位置,如何加载的web.xml文件。

说到这个问题,我们不得不提一下Tomcat启动的三大主线中的start()主线。

Tomcat 层级调用组件的start()方法,执行到StandardContext.startInternal() 时, 在startInternal()方法中调用fireLifecycleEvent()发布一个"configure_start" 事件.

public static final String CONFIGURE_START_EVENT = "configure_start";
  // Notify our interested LifecycleListeners
  fireLifecycleEvent(Lifecycle.CONFIGURE_START_EVENT, null);
  //响应configure_start事件
  protected void fireLifecycleEvent(String type, Object data) {
        LifecycleEvent event = new LifecycleEvent(this, type, data);
        for (LifecycleListener listener : lifecycleListeners) {
            listener.lifecycleEvent(event);
        }
    }

web.xml之旅就此开始。

在众多监听器中,有一个ContextConfig监听器,在监听到"configure_start" 事件后, 会执行configureStart()方法. 在configureStart()方法中执行webConfig()开始web.xml解析.

lifecycleEvent()==》
public void lifecycleEvent(LifecycleEvent event) {
    //如果事件类型是configure_start,执行 configureStart()
    if (event.getType().equals(Lifecycle.CONFIGURE_START_EVENT)) {
        configureStart();//
    } else if (event.getType().equals(Lifecycle.BEFORE_START_EVENT)) {
        beforeStart();
    } else if (event.getType().equals(Lifecycle.AFTER_START_EVENT)) {
        ....
    }
}
configureStart()==》
 protected synchronized void configureStart() {
      webConfig();
 }
 //解析web.xml
 protected void webConfig() {
     ....
 }

webConfig:就是Tomcat加载解析web.xml的地方 方法中注释标注了1,2...步骤,详细讲解web.xml加载,解析的过程.讲的很详细,我这里不一一讲了,强烈建议大家去看看源码。 这里主要提一个两个重要的点

parseWebXml(contextWebXml, webXml, false)方法: 这个方法中有一个Digester工具,在Tomcat加载server.xml配置文件的时候就是使用了此工具,解析原理异曲同工。 此处使用WebRuleSet规则,将web.xml文件中的配置读取出来设置到webXml对象中去.configureContext(StandardContext context)方法: 将web.xml文件解析出来的各个组件设置到标准servlet上下文StandardContext中去。 其中就包括我们的filter ,servlet, listener

parseWebXml()===>
public boolean parseWebXml(InputSource source, WebXml dest,
            boolean fragment) {
  Digester digester;
        WebRuleSet ruleSet;
        if (fragment) {
            digester = webFragmentDigester;
            ruleSet = webFragmentRuleSet;
        } else {
            digester = webDigester;
            ruleSet = webRuleSet;
        }
        ....
}
 configure()===>
 private void configureContext(WebXml webxml) {
        //把配置文件中filter设置到context中
  for (FilterDef filter : webxml.getFilters().values()) {
            if (filter.getAsyncSupported() == null) {
                filter.setAsyncSupported("false");
            }
            context.addFilterDef(filter);
        }
        //把配置文件中filterMap设置到context中
        for (FilterMap filterMap : webxml.getFilterMappings()) {
            context.addFilterMap(filterMap);
        }
        ....
        //把配置文件中sevrlet设置到context中
        for (ServletDef servlet : webxml.getServlets().values()) {
            Wrapper wrapper = context.createWrapper();
        }
        ...
        //把配置文件中listener设置到context中
        for (String listener : webxml.getListeners()) {
            context.addApplicationListener(listener);
        }
 }

至此StandardContext已经有了我们配置的listener,fitler,servlet


2.Tomcat 执行了listener.

在加载并将filter,servlet,listener设置到contxt中后,接下来就是执行了。

还是StandardContext.startInternal()方法, 在方法的下半部分按顺序有如下操作:listenerStart() 启动所有的listener.filterStart() 启动所有的filterloadOnStartup(findChildren()) 启动所持有的servletlistenerStart() 启动众多listener,其中就包括我们配置的ContextLoaderListener监听器。

// Configure and call application event listeners
            if (ok) {
                if (!listenerStart()) {
                    log.error(sm.getString("standardContext.listenerFail"));
                    ok = false;
                }
            }
            ...
            // Configure and call application filters
            if (ok) {
                if (!filterStart()) {
                    log.error(sm.getString("standardContext.filterFail"));
                    ok = false;
                }
            } 
            ...
            // Load and initialize all "load on startup" servlets
            if (ok) {
                if (!loadOnStartup(findChildren())){
                    log.error(sm.getString("standardContext.servletFail"));
                    ok = false;
                }
            }                       


3.ContextLoaderListener.contextInitialized(event) spring的初始化.

listenerStart() 方法其实就是调用Listener的contextInitialized()方法

public boolean listenerStart() {
 ...
 for (int i = 0; i < instances.length; i++) {
            if (!(instances[i] instanceof ServletContextListener))
                continue;
            ServletContextListener listener =
                (ServletContextListener) instances[i];
            try {
                fireContainerEvent("beforeContextInitialized", listener);
                if (noPluggabilityListeners.contains(listener)) {
                    listener.contextInitialized(tldEvent);
                } else {
                  //执行listener的初始。传递ServletContextEvent参数
                    listener.contextInitialized(event);//
                }
                fireContainerEvent("afterContextInitialized", listener);
            } catch (Throwable t) {
                ExceptionUtils.handleThrowable(t);
                fireContainerEvent("afterContextInitialized", listener);
                getLogger().error
                    (sm.getString("standardContext.listenerStart",
                                  instances[i].getClass().getName()), t);
                ok = false;
            }
        }
 ...
 }

来到ContextLoaderListener.contextInitialized(ServletContextEvent event)方法中,开始初始化web应用下的IO容器。

public void contextInitialized(ServletContextEvent event) {
        this.initWebApplicationContext(event.getServletContext());
 }

initWebApplicationContext方法中,调用了createWebApplicationContext方法来构建一个上下文类,createWebApplicationContext方法中首先调用determineContextClass()来判断上下文类型决定创建哪种上下文, 通常会使用默认策略,根据ContextLoader.properties文件中配置的WebApplicationContext值创建上下文XmlWebApplicationContext对象 至此web容器实例就创建出来了

org.springframework.web.context.WebApplicationContext=org.springframework.web.context.support.XmlWebApplicationContext

紧接着调用configureAndRefreshWebApplicationContext()方法来初始化bean.就开始了spring的著名的初始化方法refresh()

public void refresh() throws BeansException, IllegalStateException {
        synchronized(this.startupShutdownMonitor) {
            this.prepareRefresh();
            ConfigurableListableBeanFactory beanFactory = this.obtainFreshBeanFactory();
            this.prepareBeanFactory(beanFactory);
            try {
                this.postProcessBeanFactory(beanFactory);
                this.invokeBeanFactoryPostProcessors(beanFactory);
                this.registerBeanPostProcessors(beanFactory);
                this.initMessageSource();
                this.initApplicationEventMulticaster();
                this.onRefresh();
                this.registerListeners();
                this.finishBeanFactoryInitialization(beanFactory);
                this.finishRefresh();
            } catch (BeansException var9) {
                if (this.logger.isWarnEnabled()) {
                    this.logger.warn("Exception encountered during context initialization - cancelling refresh attempt: " + var9);
                }
                this.destroyBeans();
                this.cancelRefresh(var9);
                throw var9;
            } finally {
                this.resetCommonCaches();
            }
        }
    }

总结:ContextLoaderListener通过实现ServletContextListener接口,继承ContextLoader加载器。 把Tomcat与spring连接到了一起。

看懂了Tomcat与spring的关系, 想想配置的DispatcherServlet。其中原理是否有门道了

Spring分别提供了用于启动WebApplicationContext的Servlet和Web容器监听器: org.springframework.web.context.ContextLoaderServlet; org.springframework.web.context.ContextLoaderListener. ContextLoaderListener,ContextLoaderServlet 其实区别不大。流程是一样的


相关文章
|
2月前
|
XML Java 测试技术
《深入理解Spring》:IoC容器核心原理与实战
Spring IoC通过控制反转与依赖注入实现对象间的解耦,由容器统一管理Bean的生命周期与依赖关系。支持XML、注解和Java配置三种方式,结合作用域、条件化配置与循环依赖处理等机制,提升应用的可维护性与可测试性,是现代Java开发的核心基石。
|
2月前
|
XML Java 应用服务中间件
【SpringBoot(一)】Spring的认知、容器功能讲解与自动装配原理的入门,带你熟悉Springboot中基本的注解使用
SpringBoot专栏开篇第一章,讲述认识SpringBoot、Bean容器功能的讲解、自动装配原理的入门,还有其他常用的Springboot注解!如果想要了解SpringBoot,那么就进来看看吧!
444 4
|
XML Java 数据格式
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
京东一面:spring ioc容器本质是什么? ioc容器启动的步骤有哪些?
|
7月前
|
XML Java 数据格式
Spring IoC容器的设计与实现
Spring 是一个功能强大且模块化的 Java 开发框架,其核心架构围绕 IoC 容器、AOP、数据访问与集成、Web 层支持等展开。其中,`BeanFactory` 和 `ApplicationContext` 是 Spring 容器的核心组件,分别定位为基础容器和高级容器,前者提供轻量级的 Bean 管理,后者扩展了事件发布、国际化等功能。
|
10月前
|
XML Java 数据格式
Spring容器的本质
本文主要讨论Spring容器最核心的机制,用最少的代码讲清楚Spring容器的本质。
|
XML Java 数据格式
Spring容器Bean之XML配置方式
通过对以上内容的掌握,开发人员可以灵活地使用Spring的XML配置方式来管理应用程序的Bean,提高代码的模块化和可维护性。
388 6
|
安全 Java 开发者
Spring容器中的bean是线程安全的吗?
Spring容器中的bean默认为单例模式,多线程环境下若操作共享成员变量,易引发线程安全问题。Spring未对单例bean做线程安全处理,需开发者自行解决。通常,Spring bean(如Controller、Service、Dao)无状态变化,故多为线程安全。若涉及线程安全问题,可通过编码或设置bean作用域为prototype解决。
297 1
|
前端开发 Java Docker
使用Docker容器化部署Spring Boot应用程序
使用Docker容器化部署Spring Boot应用程序
|
Java Spring
Spring原理学习系列之五:IOC原理之Bean加载
其实很多同学都想通过阅读框架的源码以汲取框架设计思想以及编程营养,Spring框架其实就是个很好的框架源码学习对象。我们都知道Bean是Spring框架的最小操作单元,Spring框架通过对于Bean的统一管理实现其IOC以及AOP等核心的框架功能,那么Spring框架是如何把Bean加载到环境中来进行管理的呢?本文将围绕这个话题进行详细的阐述,并配合Spring框架的源码解析。
Spring原理学习系列之五:IOC原理之Bean加载
|
5月前
|
Java Spring 容器
SpringBoot自动配置的原理是什么?
Spring Boot自动配置核心在于@EnableAutoConfiguration注解,它通过@Import导入配置选择器,加载META-INF/spring.factories中定义的自动配置类。这些类根据@Conditional系列注解判断是否生效。但Spring Boot 3.0后已弃用spring.factories,改用新格式的.imports文件进行配置。
969 0