Springmvc web项目初始化

  1. Web容器首先会读取项目中的web.xml配置文件中的两个节点:<context-param>与<listener>
  2. Web容器创建ServletContext对象即Servlet上下文,ServletContext代表整个应用,是Servlet的共享区。
  3. Web容器将<context-param>转换为键值对,并交给ServletContext
  4. 执行listener(这里拿ServletContextListen为例),ServletContextListener实现类具有两个方法:contextInitialized()、contextDestroyed(),用来监听ServletContext对象的创建与销毁,当监测到ServletContext对象的创建时,调用contextInitialized()方法,将ServletContext对象封装到方法的事件对象中:在contextInitialized()方法内部代码如下:

           /*通过事件对象获取封装的servletContext对象*/

            servletContext=ServletContextEvent.getServletContext();

             /*通过键值获得context-param的值*/

            Context-param=ServletContext.getInitParameter(“context-param的键”)

  1. 获得context-param进行相关操作,例如在项目启动的时候创建数据库
  2. 加载filter
  3. Web容器读取每个servlet设置信息(注解或传统方式配置),并生成代表对象servletconfig,此时将ServletConfig设置在servletconfig中,可以从servletconfig中获取到servletcontext对象来获取上下文信息,实例化servlet在web容器中,收到请求后,servlet开始服务,传入servletconfig到servlet的init方法中进行servlet初始化(这个过程只进行一次)

综上,web.xml加载顺序为:

context-param->listener->filter->servlet

猜你喜欢

转载自www.cnblogs.com/yangf428/p/9945833.html