028-86922220

建站动态

根据您的个性需求进行定制 先人一步 抢占小程序红利时代

使用SpringMVC如何启动初始化

今天就跟大家聊聊有关使用Spring MVC如何启动初始化,可能很多人都不太了解,为了让大家更加了解,小编给大家总结了以下内容,希望大家根据这篇文章可以有所收获。

网站建设哪家好,找创新互联公司!专注于网页设计、网站建设、微信开发、微信小程序开发、集团企业网站建设等服务项目。为回馈新老客户创新互联还提供了昆明免费建站欢迎大家使用!

Web容器初始化过程

web容器初始化的过程,其官方文档给出了这样的描述:

  1. Instantiate an instance of each event listener identified by a element in the deployment descriptor.For instantiated listener instances that implement ServletContextListener, call the contextInitialized() method.

  2. Instantiate an instance of each filter identified by a element in the deployment descriptor and call each filter instance's init() method.

  3. Instantiate an instance of each servlet identified by a element that includes a element in the order defined by the load-on-startup element values, and call each servlet instance's init() method.

其初始化的过程实际如下:

使用Spring MVC如何启动初始化

SpringMVC 的 web.xml配置

web.xml 配置代码:

 
 
 
  
  contextConfigLocation 
  classpath:applicationContext.xml 
  
 
  
  org.springframework.web.context.ContextLoaderListener 
  
 
 
  
  mvc-dispatcher 
   
   org.springframework.web.servlet.DispatcherServlet 
   
  1 
  
                                   
  
  mvc-dispatcher 
  / 
  
 

在 Servlet API中有一个ServletContextListener接口,它能够监听ServletContext对象的生命周期,实际上就是监听Web应用的生命周期。当Servlet容器启动或终止Web应用时,会触发ServletContextEvent事件,该事件由ServletContextListener来处理。在ServletContextListener接口中定义了处理ServletContextEvent 事件的两个方法contextInitialized()contextDestroyed()

ContextLoaderListener监听器的作用就是启动Web容器时,自动装配ApplicationContext的配置信息。因为它实现了ServletContextListener这个接口,在web.xml配置了这个监听器,启动容器时,就会默认执行它实现的方法。由于在ContextLoaderListener中关联了ContextLoader这个类,所以整个加载配置过程由ContextLoader来完成。

DispatchServlet初始化

在SpringMVC架构中,DispatchServlet负责请求分发,起到控制器的作用。下面详细来解释说明:

使用Spring MVC如何启动初始化

DispatchServlet类和ContextLoaderListener类的关系图:

使用Spring MVC如何启动初始化

用ContextLoaderListener初始化上下文,接着使用DispatchServlet来初始化WebMVC的上下文

看完上述内容,你们对使用Spring MVC如何启动初始化有进一步的了解吗?如果还想了解更多知识或者相关内容,请关注创新互联行业资讯频道,感谢大家的支持。


分享题目:使用SpringMVC如何启动初始化
链接地址:http://www.tsicrk.com/article/gcppdi.html

其他资讯

让你的专属顾问为你服务

1.5810s