Tomcat启动带动Spring IoC容器启动流程源码分析

Tomcat启动带动Spring IoC容器启动流程源码分析

LD is tigger forever,CG are not brothers forever, throw the pot and shine forever.
Modesty is not false, solid is not naive, treacherous but not deceitful, stay with good people, and stay away from poor people.
talk is cheap, show others the code,Keep progress,make a better result.
Survive during the day and develop at night。

目录

概述

spring的在tomcat容器中的启动过程应该是:
tomcat启动–BootStrap.start()–spring jar包里面的Catalina.start()
startInternal()–找到servlet的子类并初始化。其中包括SpringServletContainerInitializer,调用onStartup方法
启动监听器,其中ContextLoaderListener会进行 initWebApplicationContext;初始化或者说刷新ioc容器
加载子容器:loadOnStartup(findChildren()),会把DispatcherServlet进行初始化,在这个过程中也会刷新ioc。
tomcat进行其他的启动配置。

原理概述

启动Tomcat,Tomcat初始化Servlet ------> 创建Servlet实例(Tomcat)------> DispacherServlet(SpringMVC)

------> IOC初始化(Spring)------> MVC初始化(SpringMVC)------> 创建Servlet实例(Tomcat)

------> 启动Servlet服务(Tomcat)------> SpringMVC启动完成,提供服务。

web.xml:

<?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">
 
    <servlet>
        <servlet-name>springMVCDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 初始化参数 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:spring-mvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
 
    <!-- 拦截所有以do结尾的请求 -->
    <servlet-mapping>
        <servlet-name>springMVCDispatcherServlet</servlet-name>
        <url-pattern>*.do</url-pattern>
    </servlet-mapping>
 
    <display-name></display-name>
    <welcome-file-list>
        <welcome-file>index.jsp</welcome-file>
    </welcome-file-list>
</web-app>

spring-mvc.xml:

<?xml version="1.0" encoding="UTF-8"?>
 
<!--
  - Application context definition for JPetStore's business layer.
  - Contains bean references to the transaction manager and to the DAOs in
  - dataAccessContext-local/jta.xml (see web.xml's "contextConfigLocation").
  -->
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:aop="http://www.springframework.org/schema/aop"
       xmlns:tx="http://www.springframework.org/schema/tx"
       xsi:schemaLocation="
            http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
            http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
            http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd">
    <!-- 定义映射 -->
    <bean id="urlMapping"
          class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="helloWorld.do">helloWorldController</prop>
            </props>
        </property>
    </bean>
 
    <!-- 定义视图解析器,bean的id,property的name都是spring约定的 -->
    <bean id="viewResolver"
          class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass">
            <value>org.springframework.web.servlet.view.InternalResourceView</value>
        </property>
    </bean>
 
    <!-- 定义控制器 -->
    <bean id="helloWorldController"
          class="com.jpeony.springmvc.controller.HelloWorldController">
        <property name="helloWorld">
            <value>HelloWorld</value>
        </property>
        <property name="viewPage">
            <value>/WEB-INF/jsp/index.jsp</value>
        </property>
    </bean>
</beans>

在这里插入图片描述
在init()方法,加载了web.xml里关于servlet配置,比如spring-mvc.xml配置文件等等。
PropertyValues解析web.xml属性值。
HttpServletBean的initServletBean()接口方法,在FrameWorkServlet中实现。

2.IOC的初始化
Spring两大核心功能,IOC和AOP,Tomcat启动的时候,带动Servlet启动,SpringMVC是包装Servlet,所以在初始化

SpringMVC的DispacherServlet启动,带动了IOC容器启动。

相关工具如下:

分析:

小结:

主要讲述了Tomcat启动带动Spring IoC容器启动流程源码分析剖析, 里面有许多不足,请大家指正~

参考资料和推荐阅读

1.链接: 参考资料.

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

执于代码

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值