Multiple application context, multiple dispatcher servlets?

后端 未结 2 1659
小鲜肉
小鲜肉 2020-12-05 03:24

Until now I used to think that a web-application can have only one dispatcher-servlet which we define in web.xml

  • Am I right in th
2条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-05 03:54

    Can you have multiple dispatcher servlets in a single web application ?

    Of course, quoting the official documentation (bold is actually there as well!)

    A web application can define any number of DispatcherServlets. Each servlet will operate in its own namespace, loading its own application context with mappings, handlers, etc. Only the root application context as loaded by ContextLoaderListener, if any, will be shared.


    How?

    Just declare several servlets with different names but using org.springframework.web.servlet.DispatcherServlet class. Also make sure yourServletName-servlet.xml file is available.


    What is a situation we might need this in ?

    DispatcherServlet is very flexible. Not only Spring MVC uses it, but also Spring WS, Spring support for hessian, etc.


    Also, can there only be a single application context in the entire web application ?

    Answered already, also in the quoted documentation: one application context per DispatcherServlet + one main web application context.


    How can we define multiple application contexts ?

    See above, just create multiple DispatcherServlets.


    Can a dispatcher servlet exist in a non-spring application ?

    DispatcherServlet is a Spring context (Spring application) on its own, thus: no. On the hand DispatcherServlet can be declared in an application not having parent (main) application context, thus: yes.

提交回复
热议问题