CXF结合Spring开发中,如果既有CXFServlet和DispatcherServlet,一定要先开启org.springframework.web.context.ContextLoaderListener这个监听器,然后加载DispatcherServlet完之后再加载CXFServlet。
具体看web.xml配置:
<!-- 启动spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:app-context.xml</param-value>
</context-param>
<!-- 配置CXF -->
<servlet>
<servlet-name>CXFServlet</servlet-name>
<servlet-class>
org.apache.cxf.transport.servlet.CXFServlet
</servlet-class>
<load-on-startup>2</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFServlet</servlet-name>
<url-pattern>/webservice/*</url-pattern>
</servlet-mapping>
<!-- 配置spring mvc -->
<servlet>
<servlet-name>springapp</servlet-name>
<servlet-class>com.market.servlet.MyDispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>
classpath:app-web.xml
</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springapp</servlet-name>
<url-pattern>/app/*</url-pattern>
</servlet-mapping>
其中com.market.servlet.MyDispatcherServlet是自定义的servlet,是继承org.springframework.web.servlet.DispatcherServlet的servlet