How to register multiple servlets in web.xml in one Spring application

后端 未结 3 521
情书的邮戳
情书的邮戳 2020-12-04 15:36

I want to define two servlets in my Spring web.xml - one for the application html/jsp pages, and one for a web service that will be called by an external application. Here

相关标签:
3条回答
  • 2020-12-04 16:16

    As explained in this thread on the cxf-user mailing list, rather than having the CXFServlet load its own spring context from user-webservice-servlet.xml, you can just load the whole lot into the root context. Rename your existing user-webservice-servlet.xml to some other name (e.g. user-webservice-beans.xml) then change your contextConfigLocation parameter to something like:

    <servlet>
      <servlet-name>myservlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
      <servlet-name>myservlet</servlet-name>
      <url-pattern>*.htm</url-pattern>
    </servlet-mapping>
    
    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>
        /WEB-INF/applicationContext.xml
        /WEB-INF/user-webservice-beans.xml
      </param-value>
    </context-param>
    
    <servlet>
      <servlet-name>user-webservice</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      <load-on-startup>2</load-on-startup>
    </servlet>
    
    <servlet-mapping>
      <servlet-name>user-webservice</servlet-name>
      <url-pattern>/UserService/*</url-pattern>
    </servlet-mapping>
    
    0 讨论(0)
  • 2020-12-04 16:30

    Use config something like this:

    <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>/WEB-INF/applicationContext.xml</param-value>
    </context-param>
    
    <listener>
      <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>
    
    <servlet>
      <servlet-name>myservlet</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet>
      <servlet-name>user-webservice</servlet-name>
      <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
      <load-on-startup>2</load-on-startup>
    </servlet>
    

    and then you'll need three files:

    • applicationContext.xml;
    • myservlet-servlet.xml; and
    • user-webservice-servlet.xml.

    The *-servlet.xml files are used automatically and each creates an application context for that servlet.

    From the Spring documentation, 13.2. The DispatcherServlet:

    The framework will, on initialization of a DispatcherServlet, look for a file named [servlet-name]-servlet.xml in the WEB-INF directory of your web application and create the beans defined there (overriding the definitions of any beans defined with the same name in the global scope).

    0 讨论(0)
  • 2020-12-04 16:35

    I know this is a bit old but the answer in short would be <load-on-startup> both occurrences have given the same id which is 1 twice. This may confuse loading sequence.

    0 讨论(0)
提交回复
热议问题