Working with multiple dispatcher servlets in a spring application

后端 未结 3 927
心在旅途
心在旅途 2021-02-09 14:18

In my spring application, I have the following configuration classes for the spring environment:

WebAppInitializer.java

@Order(value=1)
         


        
相关标签:
3条回答
  • 2021-02-09 15:02

    You can have as many DispatcherServlets as you want. Basically what you need to do is duplicate the configuration and give the servlet a different name (else it will overwrite the previous one), and have some separate configuration classes (or xml files) for it.

    Your controllers shouldn't care in which DispatcherServlet they run neither should you include code to detect that (what if you add another, and another you would need to keep modifying your controllers to fix that).

    However while you can have multiple servlets in general there isn't much need for multiple servlets and you can handle it with a single instance of the DispatcherServlet.

    0 讨论(0)
  • 2021-02-09 15:06

    If you are using spring 3.2 or above you can go with below code.

    Make different class for all the dispacher servlet with overriding getServletName() method, to avoid same name conflicts.

    public class WebAppInitializer extends AbstractAnnotationConfigDispatcherServletInitializer {
    
            @Override
            protected Class<?>[] getRootConfigClasses() {
                // TODO Auto-generated method stub
                return new Class<?>[] { RootConfig.class };
            }
    
            @Override
            protected Class<?>[] getServletConfigClasses() {
                // TODO Auto-generated method stub
                return new Class<?>[] { WebConfig.class };
            }
    
            @Override
            protected String[] getServletMappings() {
                // TODO Auto-generated method stub
                return new String[] { "/config1/*" };
            }
        }
    
    
    
    
     public class WebAppInitializer2 extends AbstractAnnotationConfigDispatcherServletInitializer {
    
    
            @Override
            protected Class<?>[] getRootConfigClasses() {
                // TODO Auto-generated method stub
                return new Class<?>[] { RootConfig.class };
            }
    
            @Override
            protected Class<?>[] getServletConfigClasses() {
                // TODO Auto-generated method stub
                return new Class<?>[] { WebConfig2.class };
            }
    
            @Override
            protected String[] getServletMappings() {
                // TODO Auto-generated method stub
                return new String[] { "/config2/*" };
            }
    
               @Override
                protected String getServletName() {
                  // TODO Auto-generated method stub
                 return "config2";
             }
        }
    
    0 讨论(0)
  • 2021-02-09 15:13

    We can have to multiple Dispatcher Servlets, like we can have 2(or more) DispatcherServlet with 2( or more) servlets name.So the D1 and D2 could map to different URL path.Example:-

    <!-- configured by WEB-INF/mac-servlet.xml -->
    <servlet>
        <servlet-name>mac</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>
    
    <!-- configured by WEB-INF/windows-servlet.xml -->
    <servlet>
            <servlet-name>windows</servlet-name>
            <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
            <load-on-startup>1</load-on-startup>
    </servlet>
    

    URL path could be mapped like :-

    <servlet-mapping>
    <servlet-name>mac</servlet-name>
    <url-pattern>/mac/*</url-pattern>
    </servlet-mapping>
    
    <servlet-mapping>
        <servlet-name>windows</servlet-name>
         <url-pattern>/windows/*</url-pattern>
    </servlet-mapping>
    
    0 讨论(0)
提交回复
热议问题