美文网首页
Spring Web参考文档-第一章Spring Web Mvc

Spring Web参考文档-第一章Spring Web Mvc

作者: 毛不翼 | 来源:发表于2019-11-07 17:05 被阅读0次

    Web on Servlet Stack(version 5.0.15.RELEASE)

    本系列参考文档都是由spring官方参考文档翻译而来

    第一章Spring Web Mvc

    1介绍

    Spring Web MVC框架是建立在Servlet API上的,并且最初被包含在springframe work中的web框架。 正式命名为“Spring Web MVC框架”来自他的源源模块spring-webmvc,但它通常被称为“Spring MVC” .
    与Spring Web MVC类似,Spring框架5.0中引入的响应式堆栈web框架,它被命名为“Spring WebFlux”也是基于它的源模块Spring - WebFlux。本节涵盖Spring Web MVC框架, 下一节的部分涵盖Spring WebFlux
    一些与Servlet容器和Java EE版本范围的基准信息和兼容性信息可以访问Spring框架的Wiki

    1.1 (分发器)DispatcherServlet

    在Spring WebFlux中也是同样的。
    Spring MVC,像许多其它Web框架一样,被设计围绕前端控制器模型,他的核心Servlet DispatcherServlet提供了一种共享算法,用于请求处理,实际工作的共享算法由可配置的被委托的组件来执行。 该模型是灵活多样并且支持多样化的工作流。 DispatcherServlet和任何一个Servlet一样,需要根据使用Java配置或web.xml中的Servlet规范来声明和映射。 反过来DispatcherServlet的使用Spring配置来发现他需要请求映射,视图解析,异常处理等等的委托组件。下面是Java 配置的一个例子,并注册和初始化DispatcherServlet。这个类被Servlet 容器自动检测到(参见Servlet的配置):

    // JAVA 代码
    public class MyWebApplicationInitializer implements WebApplicationInitializer{
        @Override
        public void onStartup(ServletContext servletCxt){ 
        // Load Spring web application configuration
        AnnotationConfigWebApplicationContext ac=new AnnotationConfigWebApplicationContext();
        ac.register(AppConfig.class);
        ac.refresh();
        // Create and register the Dispatcher Servlet
        DispatcherServlet servlet = new DispatcherServlet(ac);
        ServletRegistration.Dynamic registration = servletCxt.addServlet("app",servlet);
        registration.setLoadOnStartup(1);
        registration.addMapping("/app/*");
        }
    }
    
    //Kotlin代码
      class MyWebApplicationInitializer : WebApplicationInitializer {
      override fun onStartup(servletCxt: ServletContext) {
      // Load Spring web application configuration
      val ac = AnnotationConfigWebApplicationContext()
      ac.register(AppConfig::class.java)
      ac.refresh()
      // Create and register the DispatcherServlet
      val servlet = DispatcherServlet(ac)
      val registration = servletCxt.addServlet("app", servlet)
      registration.setLoadOnStartup(1)
      registration.addMapping("/app/*")
      }
    }
    

    除了能直接用ServletContext的API之外,你也能可以继承AbstractAnnotationConfigDispatcherServletInitializer同时复写特定的方法。
    接下来的例子是用web.xml来进行配置并且并注册和初始化DispatcherServlet的。

    <web-app>
      <listener>
      <listenerclass>org.springframework.web.context.ContextLoaderListener</listener-class>
      </listener>
      <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/app-context.xml</param-value>
      </context-param>
      <servlet>
      <servlet-name>app</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servletclass>
      <init-param>
      <param-name>contextConfigLocation</param-name>
      <param-value></param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
      </servlet>
      <servlet-mapping>
      <servlet-name>app</servlet-name>
      <url-pattern>/app/*</url-pattern>
      </servlet-mapping>
    </web-app>
    

    1.1.1上下文层级结构(Context Hierarchy)

    DispatcherServlet用他自己的配置来预测 WebApplicationContext ,WebApplicationContext有一个链接指向与他有关联的ServletContext and the Servlet. 它还绑定到 ServletContext,以便应用程序可以使用静态方法在RequestContextUtils上查找 WebApplicationContext,如果他们需要访问它。
    对于许多应用程序,拥有单个 WebApplicationContext非常简单且足够。这也可能有一个上下文层次结构,其中一个根 WebApplicationContext被多个DispatcherServlet(或其他 Servlet)实例共享,每个实例都有自己的子WebApplicationContext配置。有关详细信息,请参阅WebApplicationContext章节的上下文层次结构特征。
    The root WebApplicationContext typically contains infrastructure beans, such as data repositories
    and business services that need to be shared across multiple Servlet instances. Those beans are
    effectively inherited and can be overridden (that is, re-declared) in the Servlet-specific child
    WebApplicationContext, which typically contains beans local to the given Servlet. The following
    image shows this relationship:
    根 Web 应用程序上下文通常包含基础结构 bean,如数据存储库以及需要跨多个 Servlet 实例共享的业务服务。那些Beans是有效继承,并可以重写(即重新声明)在 Servlet 特定的子级WebApplicationContext,它通常包含给定 Servlet 的本地 bean。下图显示此关系:


    dispatcherServlet

    相关文章

      网友评论

          本文标题:Spring Web参考文档-第一章Spring Web Mvc

          本文链接:https://www.haomeiwen.com/subject/zprvbctx.html