分析spring web mvc启动流程

一、servlet注册

我们需要在web.xml中注册DispatcherServlet,DispatcherServlet是springMVC中唯一的一个Servlet。
  • Servlet程序必须通过Servlet容器来启动运行,并且存储目录有特殊的要求,通常需要存储在<WEB应用程序目录>\WEB-INF\classes\目录中
  • Servlet程序必须在WEB应用程序的web.xml文件中进行注册和映射其访问路径,才可以被Servlet引擎加载和被外界访问

配置web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
         version="4.0">

  <servlet>
    <servlet-name>springMVC</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
    <async-supported>true</async-supported>
  </servlet>
  
  <servlet-mapping>
    <servlet-name>springMVC</servlet-name>
    <url-pattern>/</url-pattern>
  </servlet-mapping>
  
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>/WEB-INF/applicationContext.xml</param-value>
  </context-param>
  
  <listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
  
</web-app>

二、IoC容器创建

Spring并不是天生就能在Web容器中起作用的。我们同样需要把我们的IoC容器手动添加到Web容器中。
当配置完成之后,启动tomcat。就会在IoC容器中生成这些处理我们请求的bean。DispatcherServlet和ContextLoaderListener提供了Web容器中对Spring的入口。ServletContext为Spring的IoC容器提供了一个宿主环境,在这个宿主环境中建立起一个IoC容器的体系。这个IoC容器体系是通过ContextLoaderListener的初始化来建立的。然后,把DispatcherServlet这几个Servlet作为Spring MVC处理Web请求的转发器建立起来。
根据xml文件创建IoC容器

//classpath
ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext.xml");
//文件路径
ApplicationContext getApplicationContext = new FileSystemXmlApplicationContext("配置文件的绝对路径");

根据注解创建IoC文件

ApplicationContext applicationContext = new AnnotationConfigApplicationContext(jdbc.StudentHomeWorkJdbc.class);

三、bean初始化

bean的配置

a) 通过xml文件配置

定义bean

public class StudentHomeworkJdbc{
	//
}

xml配置

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context https://www.springframework.org/schema/context/spring-context.xsd">

	<bean class="jdbc.StudentHomeworkJdbc" id="jdbc"/>

</beans>

b) 通过注释方式配置
在spring配置文件中开启注解扫描

<context:component-scan base-package="org.example.spring.mvc"/>

使用注解声明bean

@Component("jdbc")
public class StudentHomeworkJdbc {
    //
}

c) 基于java类配置

  • 使用@Configuration注解需要作为配置的类,表示该类将定义Bean的元数据
  • 使用@Bean注解相应的方法,该方法名默认就是Bean的名称,该方法返回值就是Bean的对象。
  • AnnotationConfigApplicationContext或子类进行加载基于java类的配置

简单的@Configuration配置类如下所示:

@Configuration
public class AppConfig {
    @Bean
    public MyService myService() {
        return new MyServiceImpl();
    }

四、MVC的流程

  • 通过ContextLoaderListener创建父容器;
  • 将父容器放到servletContext的Attribute中;
  • 通过servlet的init()方法开始创建子容器;
  1. 获取servletContext中的父容器;
  2. 通过配置的contextClass的值查找是否已经存在一个容器;
  3. 如果上一步没有找到,则开始重新创建一个子容器;
  4. 将父容器设置为子容器的parent;
  5. 刷新子容器
    5.1.调用子类扩展方法postProcessWebApplicationContext(wac);
    5.2. 查找并调用web.xml配置的contextInitializers;
  • 通过onRefresh(wac)方法初始化requestMapping等;
  • 将初始化的子容器也丢给servletContext的Attribute;
  • 执行子类扩展的initFrameworkServlet()方法,如果没有实现就是空的;
  • springmvc初始化结束
参考帖子
https://www.cnblogs.com/54chensongxia/p/12522804.html?utm_source=tuicool&utm_medium=referral
https://blog.csdn.net/u014082714/article/details/82388931
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值